Python Pandas Series失败日期时间 [英] Python Pandas Series failure datetime

查看:150
本文介绍了Python Pandas Series失败日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这一定是大熊猫的失败,它拥有大熊猫系列(也分别是v18.1和19),如果我为系列指定日期,那么第一次将日期添加为int(错误)时,第二次将其添加为datetime(正确),我无法理解原因.

I think that this has to be a failure of pandas, having a pandas Series (v.18.1 and 19 too), if I assign a date to the Series, the first time it is added as int (error), the second time it is added as datetime(correct), I can not understand the reason.

例如具有以下代码的

import datetime as dt
import pandas as pd
series = pd.Series(list('abc'))
date = dt.datetime(2016, 10, 30, 0, 0)
series["Date_column"] =date
print("The date is {} and the type is {}".format(series["Date_column"], type(series["Date_column"])))
series["Date_column"] =date
print("The date is {} and the type is {}".format(series["Date_column"], type(series["Date_column"])))

输出为:

The date is 1477785600000000000 and the type is <class 'int'>
The date is 2016-10-30 00:00:00 and the type is <class 'datetime.datetime'>

如您所见,它第一次总是将值设置为int而不是datetime.

As you can see, the first time it always sets the value as int instead of datetime.

有人可以帮助我吗?, 预先非常感谢您, 哈维

could someone help me?, Thank you very much in advance, Javi.

推荐答案

原因是系列是对象"类型,而熊猫DataFrame(或系列)的列是同类的.您可以使用dtype(或DataFrame.dtypes)进行检查:

The reason for this is that series is an 'object' type and the columns of a pandas DataFrame (or a Series) are homogeneously of type. You can inspect this with dtype (or DataFrame.dtypes):

series = pd.Series(list('abc'))
series
Out[3]:
0    a
1    b
2    c
dtype: object

In [15]: date = dt.datetime(2016, 10, 30, 0, 0)
date
Out[15]: datetime.datetime(2016, 10, 30, 0, 0)

In [18]: print(date)
2016-10-30 00:00:00

In [17]: type(date)
Out[17]: datetime.datetime

In [19]: series["Date_column"] = date
In [20]: series

Out[20]:
0                                a
1                                b
2                                c
Date_column    1477785600000000000
dtype: object

In [22]: series.dtype

Out[22]: dtype('O')

只有通用的'object'dtype可以容纳任何python对象(在您的情况下,将datetime.datetime对象插入到Series中).

Only the generic 'object' dtype can hold any python object (in your case inserting a datetime.datetime object into the Series).

此外,Pandas系列基于Numpy数组,它们不是混合类型,并且无法达到使用Pandas DataFrames和Series或Numpy的计算优势的目的.

Moreover, Pandas Series are based on Numpy Arrays, which are not mixed types and defeats the purpose of using the computational benefit of Pandas DataFrames and Series or Numpy.

您可以改用python list()吗?还是DataFrame()?

Could you use a python list() instead? or a DataFrame()?

这篇关于Python Pandas Series失败日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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