python - Weird behaviour on Pandas df.apply() -


i have python class similar to

class myclass(namedtuple('mytuple', 'one two')):     def method1:         pass 

and pandas dataframe 4 columns. when try apply following lambda dataframe:

df.apply(lambda x: myclass(x['col1'], x['col2']), axis=1) 

i following error:

valueerror: shape of passed values (218039, 2), indices imply (218039, 4)

which same error when try return tuple on dataframe.apply(), e.g. df.apply(lambda x: (1,2), axis=1)

however, if define myclass as

class myclass2:     def __init__(self, one, two):         self.one = 1         self.two = 2        def method1():         pass 

the .apply() method returns (a dataframe n rows of myclass2 objects)

is expected behaviour? seems df.apply() treating instance of class, instantiated namedtuple, namedtuple.

edit: tested following on ipython, after comment @root

in [30]: data = pd.dataframe(np.random.random((4,5)));  in [33]: type(data) out[33]: pandas.core.frame.dataframe   in [33]: data.shape out[33]: (4, 5)  in [34]: data.apply(lambda x: (1,2),axis=1) out[34]:  0    (1, 2) 1    (1, 2) 2    (1, 2) 3    (1, 2) dtype: object 

so, no problem when starting empty dataframe

however, original data following happens

in [41]: type(data) out[41]: pandas.core.frame.dataframe in [42]: pd.__version__ out[42]: '0.19.2' in [43]: data.head(1) out[43]:         date_start            date_end  start_lng  start_lat 0 2015-12-03 16:25:18 2015-12-03 16:28:56  -8.680015  41.172069 in [44]: data.shape out[44]: (218039, 4)  in [45]: data.apply(lambda x: (1,2),axis=1) traceback (most recent call last):   file "/applications/anaconda/anaconda/envs/py36/lib/python3.6/site-packages/pandas/core/internals.py", line 4263, in create_block_manager_from_arrays mgr = blockmanager(blocks, axes)   file "/applications/anaconda/anaconda/envs/py36/lib/python3.6/site-packages/pandas/core/internals.py", line 2761, in __init__ self._verify_integrity()   file "/applications/anaconda/anaconda/envs/py36/lib/python3.6/site-packages/pandas/core/internals.py", line 2971, in _verify_integrity construction_error(tot_items, block.shape[1:], self.axes)   file "/applications/anaconda/anaconda/envs/py36/lib/python3.6/site-packages/pandas/core/internals.py", line 4233, in construction_error passed, implied))  valueerror: shape of passed values (218039, 2), indices imply (218039, 4) 

any idea might happening here?


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -