PandasError: DataFrame constructor not properly called!

4,164 views
Skip to first unread message

andreash

unread,
Oct 4, 2011, 10:38:49 AM10/4/11
to pystatsmodels
Hi there,

I'm running into an error when checking a DataFrame == None:


In [1]: import pandas

In [2]: import numpy as np

In [3]: import datetime

In [4]: DF = pandas.DataFrame(np.atleast_2d(np.arange(12)),
index=[datetime.date(1999,1,1)])

In [5]: if DF is None:
...: print "Success!"
...:

In [6]: if DF == None:
...: print "Success!"
...:
---------------------------------------------------------------------------
PandasError Traceback (most recent call
last)
/home/USER/<ipython-input-6-f8077529972e> in <module>()
----> 1 if DF == None:
2 print "Success!"
3

/home/USER/.virtualenvs/pydoas/lib/python2.7/site-packages/pandas/core/
frame.pyc in f(self, other)
82 return self._combine_series_infer(other, func)
83 else:
---> 84 return self._combine_const(other, func)
85
86 f.__name__ = name

/home/USER/.virtualenvs/pydoas/lib/python2.7/site-packages/pandas/core/
frame.pyc in _combine_const(self, other, func)
1536
1537 return self._constructor(func(self.values, other),
index=self.index,
-> 1538 columns=self.columns,
copy=False)
1539
1540 def _compare_frame(self, other, func):

/home/USER/.virtualenvs/pydoas/lib/python2.7/site-packages/pandas/core/
frame.pyc in __init__(self, data, index, columns, dtype, copy)
164 copy=copy)
165 else:
--> 166 raise PandasError('DataFrame constructor not
properly called!')
167
168 self._data = mgr

PandasError: DataFrame constructor not properly called!

Wes McKinney

unread,
Oct 4, 2011, 10:52:50 AM10/4/11
to pystat...@googlegroups.com

hi Andreas,

that's not a robust way to check for None. Try:

DF is None

DF == None actually tries to do an element-wise comparison operation
which does not produce an array, hence causing the exception:

In [7]: DF.values == None
Out[7]: False

I also recommend using datetime.datetime instead of date if at all
possible (for interoperability reasons).

best,
Wes

andreash

unread,
Oct 6, 2011, 10:49:12 AM10/6/11
to pystatsmodels
thanks, wes!

On Oct 4, 4:52 pm, Wes McKinney <wesmck...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages