gpt4 book ai didi

python - 切片不一致 [ :] behavior on Pandas Dataframes

转载 作者:太空宇宙 更新时间:2023-11-04 07:50:42 25 4
gpt4 key购买 nike

我有 2 个数据框。第一个数据框有数字作为索引。第二个数据框以日期时间作为索引。切片运算符 (:) 在这些数据帧上的行为不同。

案例一

>>> df = pd.DataFrame({'A':[1,2,3]}, index=[0,1,2])
>>> df
A
0 1
1 2
2 3
>>> df [0:2]
A
0 1
1 2

案例二

>>> a = dt.datetime(2000,1,1)
>>> b = dt.datetime(2000,1,2)
>>> c = dt.datetime(2000,1,3)
>>> df = pd.DataFrame({'A':[1,2,3]}, index = [a,b,c])
>>> df
A
2000-01-01 1
2000-01-02 2
2000-01-03 3
>>> df[a:b]
A
2000-01-01 1
2000-01-02 2

为什么最后一行在情况 1 中被排除在外,而在情况 2 中却没有?

最佳答案

不要使用它,最好使用 loc 来保持一致性:

df = pd.DataFrame({'A':[1,2,3]}, index=[0,1,2])

print (df.loc[0:2])
A
0 1
1 2
2 3

a = datetime.datetime(2000,1,1)
b = datetime.datetime(2000,1,2)
c = datetime.datetime(2000,1,3)
df = pd.DataFrame({'A':[1,2,3]}, index = [a,b,c])

print (df.loc[a:b])
A
2000-01-01 1
2000-01-02 2

为什么省略最后一行的原因可能在 docs 中找到:

With DataFrame, slicing inside of [] slices the rows. This is provided largely as a convenience since it is such a common operation.

print (df[0:2])
A
0 1
1 2

用于按日期时间选择 exact indexing被使用:

... In contrast, indexing with Timestamp or datetime objects is exact, because the objects have exact meaning. These also follow the semantics of including both endpoints.

关于python - 切片不一致 [ :] behavior on Pandas Dataframes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com