Search code examples
pythonpython-3.xpandasdatetimeseries

'Series' object has no attribute 'datetime'


I am trying to convert a column of timestamps (YYYY-MM-DD HH-MM-SS) from a pandas dataframe to seconds.

Here is my current code:

df['recorded_time'] = pd.to_datetime(df['recorded_time'])
df['timestamp'] = df['recorded_time'].datetime.total_seconds() #creating a new column

The error I keep getting is:

AttributeError: 'Series' object has no attribute 'datetime'

Can anyone point out where I may be missing a step?


Solution

  • I'm using the example you gave in a comment for the df. You cannot use regular datetime.datetime methods on pandas datetime64 values without using the .dt accessor. In addition to the example you linked to, you said that you want total_seconds to refer to the base datetime of 2019/01/01 00:00:00. A timedelta must always have some kind of reference point, otherwise it could be any arbitrary value.

    import pandas as pd
    
    df1 = pd.DataFrame({'lat':[15.13,12.14,13.215,11.1214,12.14], 
                  'lon': [38.52, 37.536,39.86,38.536,37.536],
                  'Datetime': pd.to_datetime(['2019-03-09 16:05:07',
                                              '2019-03-15 09:50:07',
                                              '2019-03-09 11:03:47',
                                              '2019-03-10 16:41:18',
                                              '2019-03-09 06:15:27']),
                  'temp':[23,22,21,22,19]})
    
    # We can just call it once to get a reference to your datum point
    base_dt = pd.to_datetime('2019/01/01 00:00:00')
    
    df1['seconds'] = (df1['Datetime'] - base_dt).dt.total_seconds()