3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
這種行為是預(yù)期的(直到今天我也不知道)
這種類型的切片也適用于具有 DatetimeIndex 的 DataFrame。由于部分字符串選擇是標(biāo)簽切片的一種形式,因此將包括端點(diǎn)。這將包括包含日期的匹配時(shí)間:來自http://pandas-docs.github.io/pandas-docs-travis/user_guide/timeseries.html#indexing。
關(guān)于標(biāo)簽切片行為
請(qǐng)注意,與通常的 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貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
與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
添加回答
舉報(bào)