Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Series TypeError and ValueError when using datetime

Tags:

python

pandas

The code below works just fine (as expected):

import dateutil
from pandas import Series
timestamp = dateutil.parser.parse("2014-09-30 00:00:00") 
ser = Series()
ser['no_num'] = 'string is fine'
ser['time'] = timestamp
# works

ser = Series()
ser['time'] = timestamp
# works

But once ser['no_num'] is set to a number, it raises TypeError:

ser = Series()
ser['no_num'] = 5.0
ser['time'] = timestamp
# TypeError: invalid type promotion

Things get weirder if you assign timestamp when the index is defined at first:

ser = Series(index=['time'])
ser['time'] = timestamp
# ValueError: ['t' 'i' 'm' 'e'] not contained in the index

Is this a bug or somehow an expected behavior?

My Python is 3.4.1 and Pandas is 0.14.1.

like image 518
akai Avatar asked Jun 12 '26 21:06

akai


1 Answers

Series are single-dtyped. So putting different dtypes in a single container while possible is not recommended. The series will change dtype to accomodate the dtypes as you add them (which FYI is not efficient at all, better to pass in a list in the first place).

Your example fails because the Series is already a float dtype and cannot hold a Timestamp which is an object.

You can do this if you really want.

In [42]: ser = Series([5.0,timestamp],['no_num','time'])

In [43]: ser
Out[43]: 
no_num                      5
time      2014-09-30 00:00:00
dtype: object
like image 147
Jeff Avatar answered Jun 15 '26 10:06

Jeff