fullstacked
fullstacked

Reputation: 95

'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?

Upvotes: 5

Views: 61612

Answers (2)

cottontail
cottontail

Reputation: 23171

One method to convert datetime into numbers is to view it as 'int64':

df['ns_since_UNIX_epoch'] = df['recorded_time'].view('int64')

# this is equivalent to 
(df['recorded_time'] - pd.Timestamp('1970-01-01')).dt.total_seconds() * 10**9

In general, to access datetime methods, use the .dt accessor on a pandas column; and to access string methods, use the .str accessor.

To see a list of all attributes of a pandas column or Series, call dir(). You can check the following:

'datetime' in dir(pd.Series)          # False
'dt' in dir(pd.Series)                # True
'total_seconds' in dir(pd.Series.dt)  # True

Upvotes: 2

roganjosh
roganjosh

Reputation: 13175

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()

Upvotes: 10

Related Questions