Add_datepart 'Elapsed' and time of day

(Anders) #1

Hi there, I’m wondering about datepart what the new column Elapsed contains. Glancing at the source code in didn’t help me.

What I am getting at is: I would like to include time of day (hours and minutes) as part as my dataset. I’m working with the number of parked cars in a group of public parking garages, and I have new data points every 5 minutes. My reason is that the time of day is important for estimating y (number of parked cars).

Should I manually create the columns Hourofday [0…23] and MinuteofHour[0…59] ?

(Aditya) #2

Just modify that version…
Create a copy of that function in the same file with another name and just change it a bit…

(Anders) #3

Given a dataframe with column ‘date’

(0   2018-03-06 13:25:01
 1   2018-03-06 12:35:01
 2   2018-03-06 12:35:01
 Name: date, dtype: datetime64[ns], pandas.core.series.Series)

I solved my problem with:

dfCats[‘Hourofday’] = dfCats.apply(lambda row: row[‘date’].to_pydatetime().hour , axis=1)
dfCats[‘Minutesofhour’] = dfCats.apply(lambda row: row[‘date’].to_pydatetime().minute , axis=1)

Now I have two new columns named Hourofday and Minutesofhour, slice:

dfCats[‘Hourofday’][0:3], dfCats[‘Minutesofhour’][0:3]

(0 13
1 12
2 12
Name: Hourofday, dtype: int64,
0 25
1 35
2 35
Name: Minutesofhour, dtype: int64)