0

I have a pandas DF with datetime index.

Created_Ts           value        
2001-01-01 06:00:00  199
2001-01-01 06:15:00  299
2001-01-01 07:30:00  399
2001-01-01 07:45:00  400

I want to create the hour of the day from the datetime index.

Current Approach:

df = df.reset_index()
df["hour"] = df["Created_Ts"].dt.hour

It works, but is there a pandas way to create the hour column directly and without resetting the index

data_person
  • 4,194
  • 7
  • 40
  • 75
  • See [this answer](https://stackoverflow.com/a/54113327/9209546) on the duplicate. – jpp Jan 14 '19 at 09:44

1 Answers1

0

Use index and remove .dt:

df["hour"] = df.index.hour
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252