From 93764ef59b9980b9397dda49b5159ac619477258 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 19:26:40 -0800 Subject: [PATCH 1/2] CLN: annotate ndim, test cleanups --- pandas/_libs/indexing.pyx | 2 +- pandas/_libs/sparse.pyx | 4 ++-- pandas/core/base.py | 2 +- pandas/core/computation/ops.py | 2 +- pandas/core/generic.py | 2 +- pandas/core/internals/managers.py | 2 +- pandas/tests/scalar/period/test_period.py | 10 ++++++++++ pandas/tests/scalar/timestamp/test_timestamp.py | 4 ---- pandas/tests/scalar/timestamp/test_timezones.py | 5 ++--- 9 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pandas/_libs/indexing.pyx b/pandas/_libs/indexing.pyx index 308e914b7b5b7..7a25a52c7e608 100644 --- a/pandas/_libs/indexing.pyx +++ b/pandas/_libs/indexing.pyx @@ -11,7 +11,7 @@ cdef class _NDFrameIndexerBase: self._ndim = None @property - def ndim(self): + def ndim(self) -> int: # Delay `ndim` instantiation until required as reading it # from `obj` isn't entirely cheap. ndim = self._ndim diff --git a/pandas/_libs/sparse.pyx b/pandas/_libs/sparse.pyx index 578995a3eb3b6..3a0cccbf2323f 100644 --- a/pandas/_libs/sparse.pyx +++ b/pandas/_libs/sparse.pyx @@ -57,7 +57,7 @@ cdef class IntIndex(SparseIndex): return output @property - def nbytes(self): + def nbytes(self) -> int: return self.indices.nbytes def check_integrity(self): @@ -348,7 +348,7 @@ cdef class BlockIndex(SparseIndex): return output @property - def nbytes(self): + def nbytes(self) -> int: return self.blocs.nbytes + self.blengths.nbytes @property diff --git a/pandas/core/base.py b/pandas/core/base.py index 10e7b5d186bba..8e7cecea84f79 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -711,7 +711,7 @@ def shape(self): return self._values.shape @property - def ndim(self): + def ndim(self) -> int: """ Number of dimensions of the underlying data, by definition 1. """ diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index fe74b6994be7c..8fab5bd87d4fe 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -167,7 +167,7 @@ def name(self): return self._name @property - def ndim(self): + def ndim(self) -> int: return self._value.ndim diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2468c43337d0d..c8ce7561a12b8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -569,7 +569,7 @@ def axes(self): return [self._get_axis(a) for a in self._AXIS_ORDERS] @property - def ndim(self): + def ndim(self) -> int: """ Return an int representing the number of axes / array dimensions. diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index fbe1db1c23cdb..6408da37d4343 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -166,7 +166,7 @@ def shape(self): return tuple(len(ax) for ax in self.axes) @property - def ndim(self): + def ndim(self) -> int: return len(self.axes) def set_axis(self, axis, new_labels): diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 3bdf91cbf838b..73371c48f9370 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -1044,6 +1044,7 @@ def test_add_sub_nat(self): assert NaT - p is NaT p = Period("NaT", freq="M") + assert p is NaT assert p + NaT is NaT assert NaT + p is NaT assert p - NaT is NaT @@ -1284,6 +1285,7 @@ def test_add_offset_nat(self): # freq is DateOffset for freq in ["A", "2A", "3A"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [offsets.YearEnd(2)]: assert p + o is NaT assert o + p is NaT @@ -1300,6 +1302,7 @@ def test_add_offset_nat(self): for freq in ["M", "2M", "3M"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]: assert p + o is NaT assert o + p is NaT @@ -1317,6 +1320,7 @@ def test_add_offset_nat(self): # freq is Tick for freq in ["D", "2D", "3D"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [ offsets.Day(5), offsets.Hour(24), @@ -1340,6 +1344,7 @@ def test_add_offset_nat(self): for freq in ["H", "2H", "3H"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [ offsets.Day(2), offsets.Hour(3), @@ -1439,6 +1444,7 @@ def test_sub_offset_nat(self): # freq is DateOffset for freq in ["A", "2A", "3A"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [offsets.YearEnd(2)]: assert p - o is NaT @@ -1453,6 +1459,7 @@ def test_sub_offset_nat(self): for freq in ["M", "2M", "3M"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]: assert p - o is NaT @@ -1468,6 +1475,7 @@ def test_sub_offset_nat(self): # freq is Tick for freq in ["D", "2D", "3D"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [ offsets.Day(5), offsets.Hour(24), @@ -1489,6 +1497,7 @@ def test_sub_offset_nat(self): for freq in ["H", "2H", "3H"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [ offsets.Day(2), offsets.Hour(3), @@ -1511,6 +1520,7 @@ def test_sub_offset_nat(self): @pytest.mark.parametrize("freq", ["M", "2M", "3M"]) def test_nat_ops(self, freq): p = Period("NaT", freq=freq) + assert p is NaT assert p + 1 is NaT assert 1 + p is NaT assert p - 1 is NaT diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 652dd34ca7ce2..f9fa80644d4b9 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -202,8 +202,6 @@ def test_constructor(self): base_expected = 1404205200000000000 # confirm base representation is correct - import calendar - assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected tests = [ @@ -275,8 +273,6 @@ def test_constructor_with_stringoffset(self): base_expected = 1404205200000000000 # confirm base representation is correct - import calendar - assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected tests = [ diff --git a/pandas/tests/scalar/timestamp/test_timezones.py b/pandas/tests/scalar/timestamp/test_timezones.py index 424b0c9abdef8..250f48b7e711b 100644 --- a/pandas/tests/scalar/timestamp/test_timezones.py +++ b/pandas/tests/scalar/timestamp/test_timezones.py @@ -306,15 +306,14 @@ def test_astimezone(self, tzstr): @td.skip_if_windows def test_tz_convert_utc_with_system_utc(self): - from pandas._libs.tslibs.timezones import maybe_get_tz # from system utc to real utc - ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) + ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) # from system utc to real utc - ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) + ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) From 7e4fb2ff4fea386ebf1c916afa9ecc407c820680 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 19:28:30 -0800 Subject: [PATCH 2/2] revert --- pandas/_libs/indexing.pyx | 2 +- pandas/_libs/sparse.pyx | 4 ++-- pandas/core/base.py | 2 +- pandas/core/computation/ops.py | 2 +- pandas/core/generic.py | 2 +- pandas/core/internals/managers.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/indexing.pyx b/pandas/_libs/indexing.pyx index 7a25a52c7e608..308e914b7b5b7 100644 --- a/pandas/_libs/indexing.pyx +++ b/pandas/_libs/indexing.pyx @@ -11,7 +11,7 @@ cdef class _NDFrameIndexerBase: self._ndim = None @property - def ndim(self) -> int: + def ndim(self): # Delay `ndim` instantiation until required as reading it # from `obj` isn't entirely cheap. ndim = self._ndim diff --git a/pandas/_libs/sparse.pyx b/pandas/_libs/sparse.pyx index 3a0cccbf2323f..578995a3eb3b6 100644 --- a/pandas/_libs/sparse.pyx +++ b/pandas/_libs/sparse.pyx @@ -57,7 +57,7 @@ cdef class IntIndex(SparseIndex): return output @property - def nbytes(self) -> int: + def nbytes(self): return self.indices.nbytes def check_integrity(self): @@ -348,7 +348,7 @@ cdef class BlockIndex(SparseIndex): return output @property - def nbytes(self) -> int: + def nbytes(self): return self.blocs.nbytes + self.blengths.nbytes @property diff --git a/pandas/core/base.py b/pandas/core/base.py index 8e7cecea84f79..10e7b5d186bba 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -711,7 +711,7 @@ def shape(self): return self._values.shape @property - def ndim(self) -> int: + def ndim(self): """ Number of dimensions of the underlying data, by definition 1. """ diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index 8fab5bd87d4fe..fe74b6994be7c 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -167,7 +167,7 @@ def name(self): return self._name @property - def ndim(self) -> int: + def ndim(self): return self._value.ndim diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c8ce7561a12b8..2468c43337d0d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -569,7 +569,7 @@ def axes(self): return [self._get_axis(a) for a in self._AXIS_ORDERS] @property - def ndim(self) -> int: + def ndim(self): """ Return an int representing the number of axes / array dimensions. diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 6408da37d4343..fbe1db1c23cdb 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -166,7 +166,7 @@ def shape(self): return tuple(len(ax) for ax in self.axes) @property - def ndim(self) -> int: + def ndim(self): return len(self.axes) def set_axis(self, axis, new_labels):