python - 如何测试对象是否为 pandas 日期时间索引?

标签 python datetime indexing pandas

如果我在一个我知道有日期时间索引的 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/

相关文章:

ios - Darwin 标准库中的日期给我错误的日期?

Magento CE 1.4 索引器 - 索引管理

python - 在 O(1) 中删除 deque 中的散列节点

python - 在 Django 中导入 python 模块时出错

python - 如何在 Python 中将变量设为 'raw text'?

php - 计算日期之间的天数但显示负 PHP

c++ - 在 C++ 中获取 DATETIME 戳的最简单方法

haskell - 来自 GHC 的更多描述性错误消息

grails - 如何从生成的 Controller 中调用默认 Controller 的索引 Action ,或者如何从 Controller 中呈现索引 View ?

python - 加快文件解析速度