Reputation: 2052
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.
Upvotes: 3
Views: 2753
Reputation: 128958
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
Upvotes: 4