kmt
kmt

Reputation: 11

TypeError when changing an integer data series in pandas

I'm having trouble changing an integer series in a DataFrame. The problem occurs when I try to change part of an integer series to an equal-length part of another integer series.

Here's simple code that reproduces my problem:

import pandas as pd
import numpy as np
a = np.arange(10,dtype=np.int)
b = a**2 - 10*a + 12
df = pd.DataFrame({'a':a,'b':b,'a_float':a.astype(np.float),
                   'b_float':b.astype(np.float)})
print df.dtypes

a         int64
a_float   float64
b         int64
b_float   float64

cond = df.b<0
df.b[cond] = 7  #works, as expected
df.b_float[cond] = df.a_float[cond] #works
df.b[cond] = df.a[cond] #gives a TypeError

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-518b5957d002> in <module>()
----> 1 df.b[cond] = df.a[cond]

Users/kmt/Envs/scilab/lib/python2.7/site-packages/pandas-0.11.0.dev_eb3134f-py2.7-macosx-     10.7-x86_64.egg/pandas/core/series.pyc in __setitem__(self, key, value)
    740         if _is_bool_indexer(key):
    741             key = _check_bool_indexer(self.index, key)
--> 742             self.where(~key,value,inplace=True)
    743         else:
    744             self._set_with(key, value)

/Users/kmt/Envs/scilab/lib/python2.7/site-packages/pandas-0.11.0.dev_eb3134f-py2.7-macosx-    10.7-x86_64.egg/pandas/core/series.pyc in where(self, cond, other, inplace)
    681             raise ValueError('Length of replacements must equal series length')
    682 
--> 683         np.putmask(ser, ~cond, other)
    684 
    685         return None if inplace else ser

TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule  'safe'

Note that

b[cond] = a[cond]
df.b = b

works.

Any suggestions?

Thanks!

Upvotes: 1

Views: 1125

Answers (1)

Jeff
Jeff

Reputation: 129018

you hit this bug we just discovered recently. see here: https://github.com/pydata/pandas/issues/2746. Unfortunately this is a bit tricky. You want to do inplace setting with a boolean int (that doesn't need filling). I'll link to this example and see.

Upvotes: 1

Related Questions