Python Pandas system crash time

I think it should be a pandas failure 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 an int (error), the second time it is added as a date- time (right), I can not understand the reason.

For example, using 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"]))) 

Output:

 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.

Can someone help me? Thanks in advance, Xavi.

+7
python pandas datetime series
source share
1 answer

The reason for this is that the series is an “object” and the pandas DataFrame (or series) columns are uniform in type. You can check 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') 

Only the generic dtype type of an object can contain any python object (in your case, inserting a datetime.datetime object into a series).

In addition, pandas Series are based on Numpy arrays, which are not mixed types and fail to take advantage of the computational advantages of pandas DataFrames and Series or Numpy.

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

0
source share

All Articles