Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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')]

Solution 2:

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

Post a Comment for "Pandas Dataframe Date Series To List Conversion"