-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
BUG: Series construction sharing data with DTI #22113
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs a test
doc/source/whatsnew/v0.24.0.txt
Outdated
@@ -434,7 +434,7 @@ Indexing | |||
- Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`) | |||
- Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`) | |||
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`) | |||
|
|||
- Bug when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same underneath data with that index (:issue:`21907`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need the word underneath.
@@ -206,7 +206,11 @@ def __init__(self, data=None, index=None, dtype=None, name=None, | |||
data = data.astype(dtype) | |||
else: | |||
# need to copy to avoid aliasing issues | |||
data = data._values.copy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't passing deep=True simply solve this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem only occurs with tz-aware cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a good solution here, to special case things. pass deep=True
here; if this doesn't work, then the copy constructor is broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this bug is caused by the return value of _values. In datetimes.py, _values returns the DatetimeIndex itself for tz-aware and ndarray for tz-naive.
@property
def _values(self):
# tz-naive -> ndarray
# tz-aware -> DatetimeIndex
if self.tz is not None:
return self
else:
return self.values
That 's why this problem only occurs with tz-aware cases as jbrockmendel said.
In [2]: dti1 = pd.date_range('2016-01-01', periods=10, tz='US/Pacific')
In [3]: dti2 = pd.date_range('2016-01-01', periods=10)
In [4]: ser1 = pd.Series(dti1)
In [5]: ser2 = pd.Series(dti2)
In [6]: ser1[::3]=pd.NaT
In [7]: ser2[::3]=pd.NaT
In [8]: dti1
Out[8]:
DatetimeIndex([ 'NaT', '2016-01-02 00:00:00-08:00',
'2016-01-03 00:00:00-08:00', 'NaT',
'2016-01-05 00:00:00-08:00', '2016-01-06 00:00:00-08:00',
'NaT', '2016-01-08 00:00:00-08:00',
'2016-01-09 00:00:00-08:00', 'NaT'],
dtype='datetime64[ns, US/Pacific]', freq='D')
In [9]: dti2
Out[9]:
DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04',
'2016-01-05', '2016-01-06', '2016-01-07', '2016-01-08',
'2016-01-09', '2016-01-10'],
dtype='datetime64[ns]', freq='D')
also pls for all indices via the indices fixture, we DO test construction on all indices, so maybe you can just add onto these tests, look in pandas/tests/series/test_constructor.py |
can you rebase and update |
ab8376b
to
fd9c8a6
Compare
Hello @makbigc! Thanks for updating the PR.
|
fd9c8a6
to
388a21a
Compare
Codecov Report
@@ Coverage Diff @@
## master #22113 +/- ##
==========================================
+ Coverage 92.23% 92.28% +0.05%
==========================================
Files 161 161
Lines 51408 51451 +43
==========================================
+ Hits 47416 47483 +67
+ Misses 3992 3968 -24
Continue to review full report at Codecov.
|
closing as superseded by #24096 |
add a type check for DatetimeIndex and then pass a deep copy to create a Series object
closes #21907