Skip to main content
 首页 » 编程设计

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

2025年05月04日14qlqwjy

我有 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.