Pandas 是 Period 内的时间戳
Pandas is a Timestamp within a Period
我有时间戳
tstamp = pd.to_datetime('01/11/2017')
tstamp
->> Timestamp('2017-01-11 00:00:00')
我有月经
per = pd.Period('01/03/2017', 'M')
per
->> Period('2017-01', 'M')
我想知道我的时间戳是否在我的经期之内。例如 2017 年 1 月 11 日(我的时间戳)在 2017 年 1 月期间(我的 per)
tstamp in per # doesn't work
pd.Series(tstamp).isin([per])
->> TypeError: object of type 'pandas._period.Period' has no len()
似乎无法弄清楚,我宁愿不做这样的事情:
tstamp < per.to_timestamp()+pd.Timedelta(1,'M') & tstamp > per.to_timestamp()
仅仅是因为它不能很好地处理我对数据所做的其余时间计算。
您可以使用 period.start_time
和 period.end_time
属性:
In [134]: per.start_time
Out[134]: Timestamp('2017-01-01 00:00:00')
In [135]: per.end_time
Out[135]: Timestamp('2017-01-31 23:59:59.999999999')
演示:
In [128]: tstamp = pd.to_datetime(['01/11/2017', '2017-06-01'])
In [129]: tstamp
Out[129]: DatetimeIndex(['2017-01-11', '2017-06-01'], dtype='datetime64[ns]', freq=None)
In [130]: (per.start_time <= tstamp) & (tstamp <= per.end_time)
Out[130]: array([ True, False], dtype=bool)
我有时间戳
tstamp = pd.to_datetime('01/11/2017')
tstamp
->> Timestamp('2017-01-11 00:00:00')
我有月经
per = pd.Period('01/03/2017', 'M')
per
->> Period('2017-01', 'M')
我想知道我的时间戳是否在我的经期之内。例如 2017 年 1 月 11 日(我的时间戳)在 2017 年 1 月期间(我的 per)
tstamp in per # doesn't work
pd.Series(tstamp).isin([per])
->> TypeError: object of type 'pandas._period.Period' has no len()
似乎无法弄清楚,我宁愿不做这样的事情:
tstamp < per.to_timestamp()+pd.Timedelta(1,'M') & tstamp > per.to_timestamp()
仅仅是因为它不能很好地处理我对数据所做的其余时间计算。
您可以使用 period.start_time
和 period.end_time
属性:
In [134]: per.start_time
Out[134]: Timestamp('2017-01-01 00:00:00')
In [135]: per.end_time
Out[135]: Timestamp('2017-01-31 23:59:59.999999999')
演示:
In [128]: tstamp = pd.to_datetime(['01/11/2017', '2017-06-01'])
In [129]: tstamp
Out[129]: DatetimeIndex(['2017-01-11', '2017-06-01'], dtype='datetime64[ns]', freq=None)
In [130]: (per.start_time <= tstamp) & (tstamp <= per.end_time)
Out[130]: array([ True, False], dtype=bool)