Skip to content

Diff on datetime64 column evoking "ValueError: Could not convert object to NumPy datetime" #3081

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

With pandas 0.10.1 and numpy 1.7.0 (Anaconda 1.4.0), I get a ValueError when trying to use the diff method on a datetime64 column or series.

Eg:

In [3]: t = np.arange(start='2012-01-01 00:00:01', stop='2012-01-01 00:00:10', d
type='datetime64')

In [4]: df = pd.DataFrame(np.arange(len(t)))

In [5]: df['time'] = t

Using the diff method gives an error:

In [6]: df['time'].diff()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-83fe0aa0c1ed> in <module>()
----> 1 df['time'].diff()

C:\Anaconda\lib\site-packages\pandas\core\series.pyc in diff(self, periods)
   1736         diffed : Series
   1737         """
-> 1738         result = com.diff(self.values, periods)
   1739         return Series(result, self.index, name=self.name)
   1740

C:\Anaconda\lib\site-packages\pandas\core\common.pyc in diff(arr, n, axis)
    475     na_indexer = [slice(None)] * arr.ndim
    476     na_indexer[axis] = slice(None, n) if n >= 0 else slice(n, None)
--> 477     out_arr[tuple(na_indexer)] = np.nan
    478
    479     if arr.ndim == 2 and arr.dtype.name in _diff_special:

ValueError: Could not convert object to NumPy datetime

while the numpy diff works as expected:

In [7]: np.diff(df['time'].values)
Out[7]:
array([1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
       1000000000, 1000000000, 1000000000], dtype='timedelta64[ns]')

The problems seems to be that np.nan cannot be converted to a numpy datetime64.

My code also did work with a previously version of numpy and/or pandas (but am not certain wich version. Before I updated my scientific stack with anaconda).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions