-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Numpy dtype weirdness in TimedeltaIndex._simple_new #9462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
this is just to make sure it's' m8[ns] dtyped as the data has to be s view (and not actual objects) so not sure what your issue is about |
the np.array(,...) coerces the series to its base numpy repr |
So |
this is an internal routine |
...which means that
The pdb session in the OP shows that calling It looks like In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: idx = pd.Index([pd.Timedelta(1, 'h')])
In [4]: idx[0] *= 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-7a30aa64d2ec> in <module>()
----> 1 idx[0] *= 2
/Users/afni/ActivityMonitorProcessing/sojourns/source/lib/python2.7/site-packages/pandas/core/index.pyc in __setitem__(self, key, value)
894
895 def __setitem__(self, key, value):
--> 896 raise TypeError("Indexes does not support mutable operations")
897
898 def __getitem__(self, key):
TypeError: Indexes does not support mutable operations
In [5]: val = idx.values
In [6]: idx2 = pd.Index(val)
In [7]: val
Out[7]: array([3600000000000], dtype='timedelta64[ns]')
In [8]: idx
Out[8]:
<class 'pandas.tseries.tdi.TimedeltaIndex'>
['01:00:00']
Length: 1, Freq: None
In [9]: idx2
Out[9]:
<class 'pandas.tseries.tdi.TimedeltaIndex'>
['01:00:00']
Length: 1, Freq: None
In [10]: val[:] *= 2
In [11]: idx
Out[11]:
<class 'pandas.tseries.tdi.TimedeltaIndex'>
['02:00:00'] # This was supposed to be immutable!
Length: 1, Freq: None
In [12]: idx2
Out[12]:
<class 'pandas.tseries.tdi.TimedeltaIndex'>
['02:00:00']
Length: 1, Freq: None |
The I am happy that you are looking at this class. Feel free to propose some fixes. I did rip off some code from There are certainly some edge cases that are not tested / caught. |
I was poking around in tdi.py with pdb and I noticed this code at line 255 of current master:
This looks to me like it's testing whether
values
is already anndarray
, and if it isn't, it's taking a view into its values. But that's not what it's doing at all. Ifvalues
is aSeries
, it has a dtype, so the view-making code should be skipped! Except that it isn't:My tentative guess is that if the outermost layer of the dtype is a record type, then it is
True
y, and otherwise it'sFalse
y. This behavior seems too pathological to depend on.Unfortunately, that's exactly what's happening when constructing a
TimedeltaIndex
from aSeries
ofTimedelta
s:Everything seems to work, but what's actually going on is so different from what I would expect to be going on that it seems like a time bomb.
The text was updated successfully, but these errors were encountered: