Sorry for my lack of Pandas and datetime ability. I have tried but something seems to be just outside my current limited skillset.
I am using a date column (FinalStartPunch) which is typed to datetime64[ns] and if the Hour is at 0, then subtract one day from a datetime64[ns] column (date). If the FinalStartPunch hour is not a 0, then just copy the date column value as is.
sample data:
date FinalStartPunch
6/27/2015 27JUN2015:14:15:00
7/23/2015 23JUL2015:13:31:00
7/23/2015 23JUL2015:18:43:00
8/15/2015 15AUG2015:18:35:00
8/15/2015 15AUG2015:23:30:00
8/16/2015 16AUG2015:00:00:00
1/30/2016 30JAN2016:18:25:00
1/30/2016 30JAN2016:23:52:00
1/31/2016 31JAN2016:00:00:00
8/13/2016 13AUG2016:18:30:00
8/13/2016 13AUG2016:23:58:00
8/14/2016 14AUG2016:00:00:00
1/28/2017 28JAN2017:18:30:00
1/28/2017 28JAN2017:23:57:00
1/29/2017 29JAN2017:00:00:00
Key code section:
df['New'] = df['date'] - pd.Timedelta(1, unit='D') ### This one works
print(df.dtypes)
conds = [df['FinalStartPunch'].dt.hour == 0, df['FinalStartPunch'].dt.hour > 0]
choices = [df['date'] - pd.Timedelta(1, unit='D'), df['date']]
df['Date1'] = np.select(conds, choices, default=0)
The Error: invalid type promotion
The df['New'] works with the same code in the choices row which seems to be the problem causing the error.
Perhaps a Numpy issue and I need a different approach to doing this?
Any help - so greatly appreciated.
You can use
.dt.hour == 0
withnp.where()
to identify rows where the hour is 0 and- pd.tseries.offsets.Day()
to subtract a day.