2024-06-01 19:09:26 发布
网友
为什么相同的条件对一个级数是真的,而直接使用级数的相同元素是假的?你知道吗
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime('2013-11')}, index=[0]) print(df['date'] == '2013-11') print('') print(df.loc[0,'date'] == '2013-11')
我获得:
0 True Name: date, dtype: bool False
因为:
df['date']
dtype
datatime64[ns]
pandas.Series
df['date'] == '2013-11'
pandas.Series.__eq__
Pandas所做的应该是帮助您认识到您正在将字符串与series的dtypedatetime进行比较。因此它尝试将字符串解析为有用的内容,然后进行比较。这是熊猫提供的附加值的一部分。你知道吗
series
datetime
因为:
df['date']
是dtype
datatime64[ns]
的pandas.Series
df['date'] == '2013-11'
是对pandas.Series.__eq__
的调用,当Pandas编写该方法时,它可以做任何它想做的事情。你知道吗Pandas所做的应该是帮助您认识到您正在将字符串与
series
的dtype
datetime
进行比较。因此它尝试将字符串解析为有用的内容,然后进行比较。这是熊猫提供的附加值的一部分。你知道吗相关问题 更多 >
编程相关推荐