如果我在一个我知道有日期时间索引的 DataFrame
上使用 type
,我会得到:
In [17]: type(df.index)
Out[17]: pandas.tseries.index.DatetimeIndex
但是当我测试它时,我得到:
In [18]: type(df.index) == 'pandas.tseries.index.DatetimeIndex'
Out[18]: False
我知道我假设类型的类型是字符串,但我真的不知道还能尝试什么,而且搜索没有任何结果。
最佳答案
您可以使用 isinstance DatetimeIndex 类的:
In [11]: dates = pd.date_range('20130101', periods=6)
In [12]: dates
Out[12]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-01 00:00:00, ..., 2013-01-06 00:00:00]
Length: 6, Freq: D, Timezone: None
In [13]: isinstance(dates, pd.DatetimeIndex)
Out[13]: True
关于python - 如何测试对象是否为 pandas 日期时间索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030174/