2

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?

2 Answers 2

1

IIUC get hours from DatetimeIndex by DatetimeIndex.hour:

timeDataArray = pd.to_datetime(df_time_series.index).hour.to_numpy()
Sign up to request clarification or add additional context in comments.

Comments

0

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

See Pandas docs for details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.