I have a dataframe p_md. The index is a DateTime. I want to make a new column called Finish where if the index is before 5PM of that day, the column value is 11PM of that day. Otherwise if it is after 5PM, the Finish column value is 11PM of the NEXT day.
What I have so far:
p_md["Finish"] = pd.Timestamp(datetime(p_md.index.year, p_md.index.month, p_md.index.day, 23, 0, 0))
p_md.loc[(p_md.index.hour > 17), "Finish"] = p_md.Finish + pd.Timedelta(days=1)
When I do this I get a TypeError stating that the datetime constructor is getting a int64Index instead of an int. So I changed the line to
p_md["Finish"] = pd.Timestamp(datetime(p_md.index.year[0], p_md.index.month[0], p_md.index.day[0], 23, 0, 0))
This compiles and runs, however it only uses the first row of the dataframe's values, presumably due to the [0].
Table Creation code request: I just read the DateTime from a csv file, but here is basically what the initial table looks like:
df = pd.DataFrame()
df['DateTime'] = pd.date_range("1/1/2017", periods=500, freq="H")
df.set_index("DateTime", inplace=True)
df["Test"] = 0