Home > database >  How to create a formatted array from the timestamp column in a pandas dataframe
How to create a formatted array from the timestamp column in a pandas dataframe

Time:07-19

I have the following pandas dataframe called df_time_series

enter image description here

Now I would like to create a formatted array from the pandas dataframe column timestamp sucht that this additional array contains only the corresponding hours of the day. This means that e.g for the four columns with timestamp [00:00:00, 00:15:00, 00:30:00, 00:45:00] a 0 should be in this additinal array. For all columns with timestamp [01:00:00, 01:15:00, 01:30:00, 01:45:00] a 1 should be in this additional array and so on.

I tried the following suggestion from here Pandas timestamp on array

import pandas as pd

timeDataArray = pd.to_datetime(df_time_series, unit='h').values

But this yields an error "ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing". Any suggestions why this error occurs and what to do to create this formatted additional array?

CodePudding user response:

IIUC get hours from DatetimeIndex by DatetimeIndex.hour:

timeDataArray = pd.to_datetime(df_time_series.index).hour.to_numpy()

CodePudding user response:

df['date_col'].dt.strftime('%H:%M:%S')

See Pandas docs for details.

  • Related