[英]Converting "Days" from Timedelta object to regular object
I'm trying to merge the two data frames below on "day", but the time delta object is preventing this.我试图在“日”合并下面的两个数据帧,但时间增量 object 阻止了这一点。 My understanding is that I should be able to then do something like
print(df['day'].days)
and get the actual day without the "days."我的理解是,我应该能够然后执行类似
print(df['day'].days)
的操作并获得没有“天数”的实际日期。
df1 df1
import pandas as pd
from itertools import product
missing = pd.DataFrame(product(range(1,31), range(1,5)), columns=['date','time_of_day'])
missing = pd.concat([missing.assign(pid=_id) for _id in vec], ignore_index=True)
#pd.DataFrame(product([vec, range(1,31), range(1,5)], names=['pid','day','time_of_day']))
missing.pid = missing.pid.astype(str)
df 2自由度 2
from timedelta import date
def random_dates(start, end, n=12):
start_u = start.value//10**9
end_u = end.value//10**9
return pd.to_datetime(np.random.randint(start_u, end_u, n), unit='s')
start = pd.to_datetime('2015-01-01')
end = pd.to_datetime('2018-01-01')
random_dates(start, end)
df = pd.DataFrame({'datestamp':random_dates(start, end)})
df['date'] = pd.to_datetime(df['datestamp']).dt.date
df['time'] = pd.to_datetime(df['datestamp']).dt.time
df = pd.concat([df.assign(pid=_id) for _id in vec], ignore_index=True)
df['last'] = df\
.sort_values('date')\
.groupby('pid')['date']\
.transform('last')
df['days'] = df['last'] - df['date']
df
df['days'].days #doesn't work
merging合并
missing.merge(df, how = 'left', on = ['pid', 'day'])
so this would convert to days as a non-timedelta object:所以这将转换为非 timedelta object 的天数:
def get_custom_str_time(x): return math.floor(x.total_seconds()/(3600*24)) def get_custom_str_time(x): return math.floor(x.total_seconds()/(3600*24))
df.insert( 0, 'day', df['days'].apply(get_custom_str_time) ) df.insert(0, 'day', df['days'].apply(get_custom_str_time) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.