From fa5e161e0323e915c921aafb421db387890495e4 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 16 Oct 2018 20:43:15 -0700 Subject: [PATCH 1/4] implement asm8, to_timedelta64 --- pandas/core/arrays/timedeltas.py | 25 ++++++++++++++++++++++++ pandas/tests/arrays/test_datetimelike.py | 24 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index 4904a90ab7b2b..eac2d0a23c798 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -355,6 +355,31 @@ def to_pytimedelta(self): """ return tslibs.ints_to_pytimedelta(self.asi8) + def to_timedelta64(self): + """ + Return numpy array with timedelta64[ns] dtype + + Returns + ------- + ndarray[timedelta64[ns]] + + Notes + ----- + This returns a view on self, not a copy. + + See also + -------- + Timedelta.to_timedelta64 + """ + return self.asi8.view('m8[ns]') + + @property + def asm8(self): + """ + Vectorized analogue of Timedelta.asm8 + """ + return self.to_timedelta64() + days = _field_accessor("days", "days", " Number of days for each element. ") seconds = _field_accessor("seconds", "seconds", diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 6bb4241451b3f..9098c15781d03 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -122,6 +122,30 @@ def test_astype_object(self): assert asobj.dtype == 'O' assert list(asobj) == list(tdi) + def test_asm8(self): + tdi = pd.TimedeltaIndex(['1 Hour', '3 Hours']) + arr = TimedeltaArrayMixin(tdi) + + expected = np.array([3600 * 1e9, 10800 * 1e9], dtype='M8[ns]') + + result = tdi.asm8 + tm.assert_numpy_array_equal(result, expected) + + result = arr.asm8 + tm.assert_numpy_array_equal(result, expected) + + def test_to_timedelta64(self): + tdi = pd.TimedeltaIndex(['1 Hour', '3 Hours']) + arr = TimedeltaArrayMixin(tdi) + + expected = np.array([3600 * 1e9, 10800 * 1e9], dtype='M8[ns]') + + result = tdi.to_timedelta64() + tm.assert_numpy_array_equal(result, expected) + + result = arr.to_timedelta64() + tm.assert_numpy_array_equal(result, expected) + class TestPeriodArray(object): From 77788b93a2b67c4a05d82e78cc39c5ea74a99deb Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 17 Oct 2018 09:55:55 -0700 Subject: [PATCH 2/4] Typo fixup --- pandas/tests/arrays/test_datetimelike.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 9098c15781d03..4b84d7af8789c 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -126,7 +126,7 @@ def test_asm8(self): tdi = pd.TimedeltaIndex(['1 Hour', '3 Hours']) arr = TimedeltaArrayMixin(tdi) - expected = np.array([3600 * 1e9, 10800 * 1e9], dtype='M8[ns]') + expected = np.array([3600, 10800], dtype='m8[ns]') * 1e9 result = tdi.asm8 tm.assert_numpy_array_equal(result, expected) @@ -138,7 +138,7 @@ def test_to_timedelta64(self): tdi = pd.TimedeltaIndex(['1 Hour', '3 Hours']) arr = TimedeltaArrayMixin(tdi) - expected = np.array([3600 * 1e9, 10800 * 1e9], dtype='M8[ns]') + expected = np.array([3600, 10800], dtype='m8[ns]') * 1e9 result = tdi.to_timedelta64() tm.assert_numpy_array_equal(result, expected) From b252ac8c348150e2ed4139b4768f649531d36662 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 21 Oct 2018 11:35:37 -0700 Subject: [PATCH 3/4] Implement DatetimeArray.asm8, to_datetime64 --- pandas/core/arrays/datetimes.py | 16 +++++++++++++++ pandas/tests/arrays/test_datetimelike.py | 25 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 0f07a9cf3c0e0..5a60bcfbac2de 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -707,6 +707,22 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'): # ---------------------------------------------------------------- # Conversion Methods - Vectorized analogues of Timestamp methods + def to_datetime64(self): + """ + Return numpy datetime64[ns] representation of self. For timezone-aware + cases, the returned array represents UTC timestamps. + + Returns + ------- + ndarray[datetime64[ns]] + """ + return self.asi8.view('M8[ns]') + + @property + def asm8(self): + """Vectorized analogue of Timestamp.asm8""" + return self.to_datetime64() + def to_pydatetime(self): """ Return Datetime Array/Index as object ndarray of datetime.datetime diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 94850dbf16809..9053081e3545b 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -92,6 +92,30 @@ def test_to_period(self, datetime_index, freqstr): # an EA-specific tm.assert_ function tm.assert_index_equal(pd.Index(result), pd.Index(expected)) + def test_asm8(self, datetime_index): + dti = datetime_index + arr = DatetimeArrayMixin(dti) + + expected = np.array([x.asm8 for x in dti], dtype='M8[ns]') + + result = dti.asm8 + tm.assert_numpy_array_equal(result, expected) + + result = arr.asm8 + tm.assert_numpy_array_equal(result, expected) + + def test_to_datetime64(self, datetime_index): + dti = datetime_index + arr = DatetimeArrayMixin(dti) + + expected = np.array([x.asm8 for x in dti], dtype='M8[ns]') + + result = dti.to_datetime64() + tm.assert_numpy_array_equal(result, expected) + + result = arr.to_timedelta64() + tm.assert_numpy_array_equal(result, expected) + @pytest.mark.parametrize('propname', pd.DatetimeIndex._bool_ops) def test_bool_properties(self, datetime_index, propname): # in this case _bool_ops is just `is_leap_year` @@ -156,6 +180,7 @@ def test_to_timedelta64(self): tm.assert_numpy_array_equal(result, expected) result = arr.to_timedelta64() + tm.assert_numpy_array_equal(result, expected) def test_to_pytimedelta(self, timedelta_index): tdi = timedelta_index From a1e11274820813348d7159cb783677a44e45c3e9 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 22 Oct 2018 11:26:16 -0700 Subject: [PATCH 4/4] typo fixup --- pandas/tests/arrays/test_datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index a02172d462dd5..eeb173dfa9bb4 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -127,7 +127,7 @@ def test_to_datetime64(self, datetime_index): result = dti.to_datetime64() tm.assert_numpy_array_equal(result, expected) - result = arr.to_timedelta64() + result = arr.to_datetime64() tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize('propname', pd.DatetimeIndex._bool_ops)