Pd.to_datetime returns an object, not a time series

Viewed 2417

I am trying to convert my column in a df into a time series. The dataset goes from March 23rd 2015-August 17th 2019 and the dataset looks like this:

                        time    1day_active_users
0  2015-03-23 00:00:00-04:00              19687.0
1  2015-03-24 00:00:00-04:00              19437.0

I am trying to convert the time column into a datetime series but it returns the column as an object. Here is the code:

data = pd.read_csv(data_path)
data.set_index('time', inplace=True)
data.index= pd.to_datetime(data.index)
data.index.dtype 

data.index.dtype returns dtype('O'). I assume this is why when I try to index an element in time, it returns an error. For example, when I run this:

data.loc['2015']

It gives me this error

KeyError: '2015'

Any help or feedback would be appreciated. Thank you.

1 Answers

As commented, the problem might be due to the different timezones. Try passing utc=True to pd.to_datetime:

df['time'] = pd.to_datetime(df['time'],utc=True)
df['time']

Test Data

                        time  1day_active_users
0  2015-03-23 00:00:00-04:00            19687.0
1  2015-03-24 00:00:00-05:00            19437.0

Output:

0   2015-03-23 04:00:00+00:00
1   2015-03-24 05:00:00+00:00
Name: time, dtype: datetime64[ns, UTC]

And then:

df.set_index('time', inplace=True)
df.loc['2015']

gives

                           1day_active_users
time                                        
2015-03-23 04:00:00+00:00            19687.0
2015-03-24 05:00:00+00:00            19437.0
Related