Skip to content

Ensure that DatetimeArray keeps reference to original data #23956

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

Merged
merged 3 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True):

ivalues = arr.view(np.int64).ravel()

result = np.empty(shape, dtype='M8[ns]')
result = np.empty(shape, dtype=NS_DTYPE)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear: this change is cosmetic. The real change is below.

iresult = result.ravel().view(np.int64)

if len(iresult) == 0:
return result
return arr.astype(NS_DTYPE, copy=copy)

unit = get_datetime64_unit(arr.flat[0])
if unit == NPY_FR_ns:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ def test_array_i8_dtype(self, tz_naive_fixture):
assert result.base is expected.base
assert result.base is not None

def test_from_array_keeps_base(self):
# Ensure that DatetimeArray._data.base isn't lost.
arr = np.array(['2000-01-01', '2000-01-02'], dtype='M8[ns]')
dta = DatetimeArray(arr)

assert dta._data is arr
dta = DatetimeArray(arr[:0])
assert dta._data.base is arr

def test_from_dti(self, tz_naive_fixture):
tz = tz_naive_fixture
dti = pd.date_range('2016-01-01', periods=3, tz=tz)
Expand Down