Home > Software design >  Convert datetime to seconds in python using pandas
Convert datetime to seconds in python using pandas

Time:09-30

How do you convert a timestamp in this format: 30/05/2022 13:05:48.360 to seconds?

This is the code I have so far:

    def getDataset(self,csvfilename):
        
        dataset={}
        
        """remove NaN and set to 0"""
        df = pd.read_csv(csvfilename, encoding='utf-8').fillna(0)
        
        """name of headers in column"""
        list_columns = df.columns.values.tolist() 
        
        """set time format"""
        time_format = '%d/%m/%Y %H:%M:%S.%f'
                

        for col in list_columns:
            dataset[col] = df[col].iloc[0:].values
            try:
                dataset[col] = [datetime.strptime(i, time_format) for i in df[col].iloc[0:].values]
            except Exception as e:
                pass
                print(e)
             
        return dataset,list_columns
                

I have tried changing dataset[col] = [datetime.strptime(i, time_format) for i in df[col].iloc[0:].values] to dataset['Timestamp'] = df.loc[:,'Timestamp].datetime.total_seconds()but it is not working. I keep getting a "'Series' object has no attribute 'datetime'" error...Timestamp is the column that the datetime format is located in my data. The rest of the columns do not have to be converted. How can I change datetime to seconds in a specific column?

Thank you

CodePudding user response:

I think you are looking for datetime.datetime.timestamp()

https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp

CodePudding user response:

The fastest way I know is to cast the datetime-values to int64 which gives you the time in ns. And then divide by 10**9.

Example df:

import pandas as pd
df = pd.DataFrame(["28/05/2022 13:05:48.360", "29/05/2022 13:05:48.360", "30/05/2022 13:05:48.360"], columns=["dates"])
df.dates = pd.to_datetime(df.dates, format='%d/%m/%Y %H:%M:%S.%f')

Then use:

df.dates.astype('int64') // 10**9

to get

0    1653743148
1    1653829548
2    1653915948
Name: dates, dtype: int64
  • Related