[英]How do I slice a pandas time series on dates not in the index?
I have a time series indexed by datetime.date.我有一个由 datetime.date 索引的时间序列。 Here are the first knots of the series:这是该系列的第一个结:
1999-12-31 0
2000-06-30 170382.118454
2000-12-29 -319260.443362
I want to slice from the beginning of the series until Dec 28th 2000, but this doesn't work since that date is not in the index (I get a KeyError when I try original_series[:datetime.date(2000,12,28)]
. I've also tried converting the index to timestamps, but that gives very spurious results (it manufactures fake knots, see below), so I wondered if there's a good approach to this problem.我想从系列的开头切到 2000 年 12 月 28 日,但这不起作用,因为该日期不在索引中(当我尝试original_series[:datetime.date(2000,12,28)]
时出现 KeyError original_series[:datetime.date(2000,12,28)]
. 我也尝试将索引转换为时间戳,但这会产生非常虚假的结果(它会制造假结,见下文),所以我想知道是否有解决这个问题的好方法。
test = pd.Series(original_series.values, map(pd.Timestamp, original_series.index))
At a first glance, this looks alright:乍一看,这看起来不错:
1999-12-31 0.000000
2000-06-30 170382.118454
2000-12-29 -319260.443362
But then I try to do my slicing (where do those extra days in January 2000 come from?):但是后来我尝试进行切片(2000 年 1 月的那些额外的日子是从哪里来的?):
In [84]: test[:'2000-12-28']
Out[84]:
1999-12-31 0.000000
2000-06-30 170382.118454
2000-01-03 -71073.979016
2000-01-04 100498.744748
2000-01-05 91104.743684
2000-01-06 82290.255459
You can simply do, if ts
is your time.serie
: 你可以简单地做,如果ts
是你的time.serie
:
In [77]: ts = pd.Series([99,65],index=pd.to_datetime(['2000-12-24','2000-12-30']))
In [78]: ts
Out[78]:
2000-12-24 99
2000-12-30 65
dtype: int64
In [79]: ts[ts.index<=pd.to_datetime('2000-12-28')]
Out[79]:
2000-12-24 99
dtype: int64
If you have index
as string
just proceed with: 如果您将index
作为string
,请继续:
ts[ts.index.map(pd.to_datetime)<=pd.to_datetime('2000-12-28')]
There is a simple way to do it without converting it into a time-series object.有一种简单的方法可以做到这一点,而无需将其转换为时间序列对象。
Scenario when your index is not a date:当您的索引不是日期时的场景:
Your df:你的df:
index date data索引日期数据
0 2000-01-01 10 0 2000-01-01 10
1 2000-01-02 20 1 2000-01-02 20
2 2000-01-03 12 2 2000-01-03 12
First, convert your date into date-time format:首先,将您的日期转换为日期时间格式:
df["date"] = pd.to_datetime(df["date"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.