python - Adding series to pandas dataframe -
i have dataframe p_md. index datetime. want make new column called finish if index before 5pm of day, column value 11pm of day. otherwise if after 5pm, finish column value 11pm of next day.
what have 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 typeerror stating datetime constructor getting int64index instead of int. changed line
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 , runs, uses first row of dataframe's values, presumably due [0].
table creation code request: read datetime csv file, here 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
if understand correctly, create "finish" as:
p_md["finish"] = p_md.index
then, use series apply (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.series.apply.html) function desired output:
p_md["finish"] = p_md["finish"].apply(lambda dt: pd.timestamp(dt.year, dt.month, dt.day, 23, 0 ,0) + pd.timedelta(days=1) if dt.hour > 17 else pd.timestamp(dt.year, dt.month, dt.day, 23, 0 ,0))
or can create separate function , assign apply:
def cvt_date(dt): new_dt = pd.timestamp(dt.year, dt.month, dt.day, 23, 0 ,0) if dt.hour > 17: new_dt = new_dt + pd.timedelta(days=1) return new_dt p_md['finish'] = p_md['finish'].apply(cvt_date)
here's original data , outputs get:
2016-03-04 03:48:41 - 2016-03-04 23:00:00
2016-02-05 22:08:25 - 2016-02-06 23:00:00
2016-12-11 19:13:54 - 2016-12-12 23:00:00
Comments
Post a Comment