AngiSen
AngiSen

Reputation: 997

Pandas DataFrame Date Series to List conversion

I have a dataframe called "signal_data" that has data as below

Pressure   DateTime             Temp
3025       2016-04-01 00:17:00  TTY    
3019       2016-04-01 00:17:00  TTY    
3019       2016-04-01 00:17:00  TTY    
.....          
3025       2016-05-01 10:17:00  TTY    
.....

I am trying to get unique values from DateTime column and convert to list. When I apply the following command, the datetime values are getting converted to int but not datetime.

signal_data['DateTime'].unique().tolist()

[1459469820000000000,    
 1459516620000000000,    
 1459527420000000000,    
 ...    
 1462087020000000000]

May I know why this is happening and how to resolve this issue?

Upvotes: 2

Views: 8832

Answers (2)

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

When you call unique(), it returns a numpy array of type datetime. Numpy datetime is not python's fundamental datatype. When you convert it to list it trys to change the datatype to pythonic version. So you get the int. So use list(..) to keep the datetime as is. i.e

list(df['DateTime'].unique()) 
[numpy.datetime64('2016-04-01T00:17:00.000000000')]

OR convert it to series and then tolist()

pd.Series(df['DateTime'].unique()).tolist()
[Timestamp('2016-04-01 00:17:00')]

Upvotes: 2

PMende
PMende

Reputation: 5460

I'm not really sure why Pandas is implicity casting it. However, you can fix it by doing the following (assuming you have done import pandas as pd):

pd.to_datetime(signal_data['DateTime'].unique()).tolist()

Upvotes: 2

Related Questions