3 回答
TA贡献1828条经验 获得超6个赞
这种行为是预期的(直到今天我也不知道)
这种类型的切片也适用于具有 DatetimeIndex 的 DataFrame。由于部分字符串选择是标签切片的一种形式,因此将包括端点。这将包括包含日期的匹配时间:来自http://pandas-docs.github.io/pandas-docs-travis/user_guide/timeseries.html#indexing。
关于标签切片行为
请注意,与通常的 python 切片相反,开始和停止都包含在 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html#pandas.DataFrame.loc
In [16]: df[df.index < '2011-01-02']
Out[16]:
B
A
2011-01-01 23
In [17]: df[df.index >= '2011-01-02']
Out[17]:
B
A
2011-01-02 33
2011-01-03 43
2011-01-04 53
In [18]: df[df.index > '2011-01-02']
Out[18]:
B
A
2011-01-03 43
2011-01-04 53
TA贡献1784条经验 获得超9个赞
与get_loc_iloc
df.iloc[:df.index.get_loc('2011-01-02')]
A B
A
2011-01-01 2011-01-01 23
df.iloc[df.index.get_loc('2011-01-02'):]
A B
A
2011-01-02 2011-01-02 33
2011-01-03 2011-01-03 43
2011-01-04 2011-01-04 53
添加回答
举报
