From 1a0f55e7bdaada7812eeaf8ee67e49a7f11d864c Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 6 Jul 2018 09:06:59 -0500 Subject: [PATCH 1/5] raises section in docstring --- pandas/_libs/tslibs/conversion.pyx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index fd4b0b11065ed..c78380c6a14eb 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -1038,6 +1038,10 @@ def normalize_date(object dt): Returns ------- normalized : datetime.datetime or Timestamp + + Raises + ------ + TypeError : if input is not datetime.date, datetime.datetime, or Timestamp """ if PyDateTime_Check(dt): if not PyDateTime_CheckExact(dt): From ef890d0f77ad8d6fcfca494bc7898cbe93cad579 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 6 Jul 2018 09:10:38 -0500 Subject: [PATCH 2/5] rename date_normalize-->normalize_i8_timestamps --- pandas/_libs/tslibs/conversion.pyx | 2 +- pandas/_libs/tslibs/timestamps.pyx | 4 ++-- pandas/core/indexes/datetimes.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index c78380c6a14eb..2d24da1a6af03 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -1060,7 +1060,7 @@ def normalize_date(object dt): @cython.wraparound(False) @cython.boundscheck(False) -def date_normalize(ndarray[int64_t] stamps, tz=None): +def normalize_i8_timestamps(ndarray[int64_t] stamps, tz=None): """ Normalize each of the (nanosecond) timestamps in the given array by rounding down to the beginning of the day (i.e. midnight). If `tz` diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 401ba76e341b2..711db7cc8fbe2 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -21,7 +21,7 @@ from util cimport (is_datetime64_object, is_timedelta64_object, INT64_MAX) cimport ccalendar -from conversion import tz_localize_to_utc, date_normalize +from conversion import tz_localize_to_utc, normalize_i8_timestamps from conversion cimport (tz_convert_single, _TSObject, convert_to_tsobject, convert_datetime_to_tsobject) from fields import get_start_end_field, get_date_name_field @@ -1083,7 +1083,7 @@ class Timestamp(_Timestamp): Normalize Timestamp to midnight, preserving tz information. """ - normalized_value = date_normalize( + normalized_value = normalize_i8_timestamps( np.array([self.value], dtype='i8'), tz=self.tz)[0] return Timestamp(normalized_value).tz_localize(self.tz) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 05f7af6383211..966eff58262e8 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -2002,7 +2002,7 @@ def normalize(self): '2014-08-01 00:00:00+05:30'], dtype='datetime64[ns, Asia/Calcutta]', freq=None) """ - new_values = conversion.date_normalize(self.asi8, self.tz) + new_values = conversion.normalize_i8_timestamps(self.asi8, self.tz) return DatetimeIndex(new_values, freq='infer', name=self.name).tz_localize(self.tz) From 5cfc1556ac132b44924833adb19b95428489a601 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 6 Jul 2018 09:20:15 -0500 Subject: [PATCH 3/5] implement test for tslibs.__init__ namespace --- pandas/tests/tslibs/test_api.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 pandas/tests/tslibs/test_api.py diff --git a/pandas/tests/tslibs/test_api.py b/pandas/tests/tslibs/test_api.py new file mode 100644 index 0000000000000..0210a0393636b --- /dev/null +++ b/pandas/tests/tslibs/test_api.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +"""Tests that the tslibs API is locked down""" + +from pandas._libs import tslibs + + +def test_namespace(): + # just check that none of these raise NameErrors + tslibs.normalize_date + tslibs.localize_pydatetime + tslibs.tz_convert_single + tslibs.NaT + tslibs.iNaT + tslibs.OutOfBoundsDatetime + tslibs.Timestamp + tslibs.Timedelta + tslibs.delta_to_nanoseconds From 58929e19d4cac05330daceb7b6215310aa711b36 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 6 Jul 2018 11:14:17 -0500 Subject: [PATCH 4/5] test namespace more precisely --- pandas/tests/tslibs/test_api.py | 39 ++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/pandas/tests/tslibs/test_api.py b/pandas/tests/tslibs/test_api.py index 0210a0393636b..56c4394438d48 100644 --- a/pandas/tests/tslibs/test_api.py +++ b/pandas/tests/tslibs/test_api.py @@ -5,13 +5,32 @@ def test_namespace(): - # just check that none of these raise NameErrors - tslibs.normalize_date - tslibs.localize_pydatetime - tslibs.tz_convert_single - tslibs.NaT - tslibs.iNaT - tslibs.OutOfBoundsDatetime - tslibs.Timestamp - tslibs.Timedelta - tslibs.delta_to_nanoseconds + + expected = [ + 'ccalendar', + 'conversion', + 'fields', + 'frequencies', + 'nattype', + 'np_datetime', + 'offsets', + 'parsing', + 'period', + 'resolution', + 'strptime', + 'timedeltas', + 'timestamps', + 'timezones', + + 'NaT', + 'iNaT', + 'OutOfBoundsDatetime', + 'Timedelta', + 'Timestamp', + 'delta_to_nanoseconds', + 'ints_to_pytimedelta', + 'localize_pydatetime', + 'normalize_date', + 'tz_convert_single'] + names = [x for x in dir(tslibs) if not x.startswith('__')] + assert set(names) == expected From 687ee7f5032c52a625197211d2a7a9dfd3904933 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 6 Jul 2018 11:17:02 -0500 Subject: [PATCH 5/5] separate api --- pandas/tests/tslibs/test_api.py | 51 +++++++++++++++++---------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/pandas/tests/tslibs/test_api.py b/pandas/tests/tslibs/test_api.py index 56c4394438d48..387a63f61179d 100644 --- a/pandas/tests/tslibs/test_api.py +++ b/pandas/tests/tslibs/test_api.py @@ -6,31 +6,32 @@ def test_namespace(): - expected = [ - 'ccalendar', - 'conversion', - 'fields', - 'frequencies', - 'nattype', - 'np_datetime', - 'offsets', - 'parsing', - 'period', - 'resolution', - 'strptime', - 'timedeltas', - 'timestamps', - 'timezones', + submodules = ['ccalendar', + 'conversion', + 'fields', + 'frequencies', + 'nattype', + 'np_datetime', + 'offsets', + 'parsing', + 'period', + 'resolution', + 'strptime', + 'timedeltas', + 'timestamps', + 'timezones'] - 'NaT', - 'iNaT', - 'OutOfBoundsDatetime', - 'Timedelta', - 'Timestamp', - 'delta_to_nanoseconds', - 'ints_to_pytimedelta', - 'localize_pydatetime', - 'normalize_date', - 'tz_convert_single'] + api = ['NaT', + 'iNaT', + 'OutOfBoundsDatetime', + 'Timedelta', + 'Timestamp', + 'delta_to_nanoseconds', + 'ints_to_pytimedelta', + 'localize_pydatetime', + 'normalize_date', + 'tz_convert_single'] + + expected = set(submodules + api) names = [x for x in dir(tslibs) if not x.startswith('__')] assert set(names) == expected