8000 BUG: Series construction sharing data with DTI by makbigc · Pull Request #22113 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: Series construction sharing data with DTI #22113

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix Bug #21907
  • Loading branch information
makbigc committed Nov 18, 2018
commit 72b7ee5b65a182e76d8bf72a8b9b4d241ac7823a
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ Indexing
- Bug where setting a timedelta column by ``Index`` causes it to be casted to double, and therefore lose precision (:issue:`23511`)
- Bug in :func:`Index.union` and :func:`Index.intersection` where name of the ``Index`` of the result was not computed correctly for certain cases (:issue:`9943`, :issue:`9862`)
- Bug in :class:`Index` slicing with boolean :class:`Index` may raise ``TypeError`` (:issue:`22533`)
- Bug when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same underneath data with that index (:issue:`21907`)

Missing
^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,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()
Copy link
Contributor

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?

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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')

# GH21907
if isinstance(data, DatetimeIndex):
data = data.copy(deep=True)
else:
data = data._values.copy()
copy = False

elif isinstance(data, np.ndarray):
Expand Down
0