什么改变了此 pandas 代码中的日期类型? [英] What changes type of date in this pandas code?

查看:57
本文介绍了什么改变了此 pandas 代码中的日期类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pandas中有一个数据框,其中包含日期和一些其他数据.日期明确地为datetime.date类型.对于这个例子,我是用手强迫的.在实际问题中,框架是从已经用这种方法设置的外部源中导入的.处理完索引后,我发现我的日期属于类pandas._libs.tslib.Timestamp,这会导致与以后的代码不兼容.是什么原因导致类型/类的变化?

I have a data frame in Pandas that has dates and some other data. The dates are explicitly of type datetime.date. For the example, I'm forcing that by hand. In the real problem, the frame is imported from an external source already set that way. After manipulating the indexing, I find that my dates are of class pandas._libs.tslib.Timestamp, which then causes incompatibilities with later code. What causes that change in type / class?

最小工作示例(请注意第3行和第7行之间的区别):

Minimal working example (note the differences between lines 3 and 7):

In [1]: df = pd.DataFrame({'date' : ['02/20/2015','01/15/2016','08/21/2015'],  'i' : ['Bob', 'Bob', 'Jim'] ,'v' : [1, 2, 3]})

In [2]: df['date'] = pd.to_datetime(df.date).dt.date

In [3]: print type(df.date[0])
<type 'datetime.date'>

In [4]: df.set_index(['i','date'], inplace=True)

In [5]: print type(df.loc['Bob',:].index[0])
<class 'pandas._libs.tslib.Timestamp'>

In [6]: df.reset_index(inplace=True)

In [7]: print type(df.date[0])
<class 'pandas._libs.tslib.Timestamp'>

推荐答案

我为此苦苦挣扎了好几个小时,最后才将问题归结为多索引问题.在原始情况下,很难发现,因为多索引操作出现在较大的操作集的中间,其中包括切片,部分索引等.但是最重要的是,日期的类型在第4行进行了转换在上面的示例中,当设置了多重索引时,此后它仍然属于Pandas类.

I struggled with this for hours and finally traced the issue to the multi-index. In the original context, it was hard to spot because the multi-index operation appeared in the middle of larger set of operations that included slicing, partial indexing, etc. But the bottom line is that the type of the dates is converted at line 4 in the example above when the multi-index is set, and it remains of the Pandas class after that.

如果我只是set_index('time'),例如常规(非多)索引,没有类型转换.这也是跟踪原因的复杂因素,因为我简化了索引操作,这是调试的第一步,从而消除了我试图跟踪的问题.

If instead I just set_index('time'), e.g. a regular (not multi) index, there's no type conversion. This was also a complicating factor in tracing the cause since I simplified the indexing operations as a first step in debugging, which eliminated the issue that I was trying to trace.

这篇关于什么改变了此 pandas 代码中的日期类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆