简体   繁体   English

Python Pandas Series失败日期时间

[英]Python Pandas Series failure 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. 我认为这必须是大熊猫的失败,有一个大熊猫系列(也是第18.1和19节),如果我给系列分配一个日期,第一次添加为int(错误),第二次它被添加为datetime(正确),我无法理解原因。

For instance with this code: 例如,使用此代码:

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 output is: 输出是:

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'>

As you can see, the first time it always sets the value as int instead of datetime. 如您所见,它第一次始终将值设置为int而不是datetime。

could someone help me?, Thank you very much in advance, Javi. 有人可以帮助我吗?非常感谢你,Javi。

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. 原因是系列是'对象'类型,而pandas DataFrame(或系列)的列是同类型的。 You can inspect this with dtype (or DataFrame.dtypes): 您可以使用dtype(或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')

Only the generic 'object' dtype can hold any python object (in your case inserting a datetime.datetime object into the Series). 只有通用的'object'dtype可以包含任何python对象(在你的情况下,将datetime.datetime对象插入到Series中)。

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. 此外,Pandas系列基于Numpy Arrays,它们不是混合类型,并且无法使用Pandas DataFrames和Series或Numpy的计算优势。

Could you use a python list() instead? 你可以使用python list()吗? or a DataFrame()? 还是一个DataFrame()?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM