Skip to content

'datetime64[ns]' columns cause fillna function fail #3047

Closed
@simomo

Description

  • pd.version: 0.11.0.dev-156e03c
  • if dataframe had a column which had been recognized as 'datetime64[ns]' type, calling the dataframe's fillna function would cause an expection.
In [149]: df_t1 = DataFrame({'int': [1, 2, None], 'date_obj': [datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 1), None], 'date_date':[datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 1)]})

In [150]: df_t1.dtypes
Out[150]:
date_date    datetime64[ns]
date_obj             object
int                 float64
dtype: object

In [151]: df_t1.fillna(np.nan)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-151-782ffd0df53e> in <module>()
----> 1 df_t1.fillna(np.nan)

/usr/local/lib/python2.6/dist-packages/pandas-0.11.0.dev_156e03c-py2.6-linux-x86_64.egg/pandas/core/frame.pyc in fillna(self, value, method, axis, inplace, limit)
   3385                 return result
   3386             else:
-> 3387                 new_data = self._data.fillna(value, inplace=inplace)
   3388 
   3389         if inplace:

/usr/local/lib/python2.6/dist-packages/pandas-0.11.0.dev_156e03c-py2.6-linux-x86_64.egg/pandas/core/internals.pyc in fillna(self, *args, **kwargs)
    944 
    945     def fillna(self, *args, **kwargs):
--> 946         return self.apply('fillna', *args, **kwargs)
    947 
    948     def astype(self, *args, **kwargs):

/usr/local/lib/python2.6/dist-packages/pandas-0.11.0.dev_156e03c-py2.6-linux-x86_64.egg/pandas/core/internals.pyc in apply(self, f, *args, **kwargs)
    915                 applied = f(blk, *args, **kwargs)
    916             else:
--> 917                 applied = getattr(blk,f)(*args, **kwargs)
    918 
    919             if isinstance(applied,list):

/usr/local/lib/python2.6/dist-packages/pandas-0.11.0.dev_156e03c-py2.6-linux-x86_64.egg/pandas/core/internals.pyc in fillna(self, value, inplace)
    212         new_values = self.values if inplace else self.values.copy()
    213         mask = com.isnull(new_values)
--> 214         np.putmask(new_values, mask, value)
    215 
    216         if inplace:

ValueError: Must be a datetime.date or datetime.datetime object

In [153]: del df_t1['date_date']

In [155]: df_t1.fillna(np.nan)
Out[155]:
date_obj    int
0    2012-01-01 00:00:00     1
1    2012-01-01 00:00:00     2
2    NaN    NaN
  • and i tried to convert the 'datetime64[ns]' column's type to object, but i failed
In [162]: df_t1.date_date = df_t1.date_date.astype(df_t1.date_obj.dtype)
In [164]: df_t1.dtypes
Out[164]:
date_date    datetime64[ns]
date_obj             object
int                 float64
dtype: object

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions