dstar
dstar

Reputation: 199

Using Pandas to create a day/ night flag

I have a pandas dataframe bikes with many columns, one of which is ts (datetime64). Format is 7/1/2015 00:00:03. I would like to create a dayflag column, which should indicate if the record belongs to the day or the night.

For the dayflag, I tried to adapt a solution provided to another date question I had posted.

bikes['dayflag'] = bikes[(bikes.ts.dt.hour > 5) & (bikes.ts.dt.hour <18)], but I get a Wrong number of items passed 18, placement implies 1 error.

Thanks in advance.

Upvotes: 0

Views: 1083

Answers (1)

user29791
user29791

Reputation: 332

I think you are trying to add a table to a column. Try to only assign the condition:

bikes['dayflag'] = (bikes.ts.dt.hour > 5) & (bikes.ts.dt.hour <18)

Upvotes: 1

Related Questions