![](/img/trans.png)
[英]Converting an object to datetime with Pandas to_datetime not working
[英]Comparing pandas to_datetime with datetime object
为什么下面的结果是 True? 日期对象有不同的类型,这种比较是如何进行的?
import pandas as pd
import datetime
df = pd.DataFrame({
'year': [2015, 2016, 2019, 2016],
'month': [2, 3, 8, 9],
'day': [4, 5, 25, 15]} )
df2 = pd.to_datetime(df)
df2
0 2015-02-04
1 2016-03-05
2 2019-08-25
3 2016-09-15
数据类型:datetime64[ns]
date1 = df2.iloc[1]
date1
时间戳('2016-03-05 00:00:00')
date2 = datetime.datetime(2016, 3, 5)
date2
datetime.datetime(2016, 3, 5, 0, 0)
date1 == date2
真的
正如@not_speshal 所写,文档很清楚。 使用模块inspect
,您可以看到pandas.Timestamp
继承自datetime.datetime
。
>>> inspect.getmro(pd.Timestamp)
(pandas._libs.tslibs.timestamps.Timestamp,
pandas._libs.tslibs.timestamps._Timestamp,
pandas._libs.tslibs.base.ABCTimestamp,
datetime.datetime,
datetime.date,
object)
有关更多详细信息,您可以从 Pandas 存储库中读取这两个文件timestamps.pyi
和timestamps.pyx
。
df2['Date']=pd.to_datetime(df2['Date'])#Coerce df2.Date to datetime assumming df2 is stand alone with own Date field
df['Date']=pd.to_datetime(df.apply(lambda x: x.astype(str).str.cat(sep='-'), axis=1))#Concat df entities to date string and coerce it to datettime
df['Date']==df2['Date']#Compare the two
结果
0 True
1 True
2 True
3 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.