From 69059e8c2c4423f334cbac33faf721fb07a15017 Mon Sep 17 00:00:00 2001 From: Shoham Debnath Date: Fri, 1 Oct 2021 01:14:45 +0530 Subject: [PATCH] TST: fixed cython doctests (#43768) --- ci/code_checks.sh | 4 ++++ environment.yml | 1 + pandas/_libs/hashtable.pyx | 8 +++++--- pandas/_libs/lib.pyx | 8 ++++++-- pandas/_libs/tslibs/dtypes.pyx | 4 ++-- pandas/_libs/tslibs/nattype.pyx | 6 +++--- pandas/_libs/tslibs/period.pyx | 2 +- pandas/_libs/tslibs/timestamps.pyx | 20 ++++++++++++-------- pandas/_libs/tslibs/timezones.pyx | 16 +++++++--------- requirements-dev.txt | 1 + 10 files changed, 42 insertions(+), 28 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 76aae2e908de4..21250789fde9f 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -84,6 +84,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then pandas/tseries/ RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Cython Doctests' ; echo $MSG + python -m pytest --doctest-cython pandas/_libs + RET=$(($RET + $?)) ; echo $MSG "DONE" + fi ### DOCSTRINGS ### diff --git a/environment.yml b/environment.yml index 733bd06fbe12f..00fb5b0580200 100644 --- a/environment.yml +++ b/environment.yml @@ -122,3 +122,4 @@ dependencies: - types-PyMySQL - types-pytz - types-setuptools + - pytest-cython diff --git a/pandas/_libs/hashtable.pyx b/pandas/_libs/hashtable.pyx index 132435701bddb..3eb7bcc673cd4 100644 --- a/pandas/_libs/hashtable.pyx +++ b/pandas/_libs/hashtable.pyx @@ -98,7 +98,8 @@ cdef class ObjectFactorizer(Factorizer): -------- Factorize values with nans replaced by na_sentinel - >>> factorize(np.array([1,2,np.nan], dtype='O'), na_sentinel=20) + >>> fac = ObjectFactorizer(3) + >>> fac.factorize(np.array([1,2,np.nan], dtype='O'), na_sentinel=20) array([ 0, 1, 20]) """ cdef: @@ -142,8 +143,9 @@ cdef class Int64Factorizer(Factorizer): -------- Factorize values with nans replaced by na_sentinel - >>> factorize(np.array([1,2,np.nan], dtype='O'), na_sentinel=20) - array([ 0, 1, 20]) + >>> fac = Int64Factorizer(3) + >>> fac.factorize(np.array([1,2,3]), na_sentinel=20) + array([0, 1, 2]) """ cdef: ndarray[intp_t] labels diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 4b1feab63dd1e..4cc8d1ac2f60e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -175,6 +175,7 @@ def is_scalar(val: object) -> bool: Examples -------- + >>> import datetime >>> dt = datetime.datetime(2018, 10, 3) >>> pd.api.types.is_scalar(dt) True @@ -256,11 +257,12 @@ def is_iterator(obj: object) -> bool: Examples -------- + >>> import datetime >>> is_iterator((x for x in [])) True >>> is_iterator([1, 2, 3]) False - >>> is_iterator(datetime(2017, 1, 1)) + >>> is_iterator(datetime.datetime(2017, 1, 1)) False >>> is_iterator("foo") False @@ -1076,11 +1078,12 @@ def is_list_like(obj: object, allow_sets: bool = True) -> bool: Examples -------- + >>> import datetime >>> is_list_like([1, 2, 3]) True >>> is_list_like({1, 2, 3}) True - >>> is_list_like(datetime(2017, 1, 1)) + >>> is_list_like(datetime.datetime(2017, 1, 1)) False >>> is_list_like("foo") False @@ -1355,6 +1358,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str: Examples -------- + >>> import datetime >>> infer_dtype(['foo', 'bar']) 'string' diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index f79ffd2d425c4..56be8bbfdcad2 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -227,7 +227,7 @@ class Resolution(Enum): Examples -------- >>> Resolution.from_attrname('second') - 2 + >>> Resolution.from_attrname('second') == Resolution.RESO_SEC True @@ -244,7 +244,7 @@ class Resolution(Enum): Examples -------- >>> Resolution.get_reso_from_freq('H') - 4 + >>> Resolution.get_reso_from_freq('H') == Resolution.RESO_HR True diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 4ff6be25127c8..521927cd910ec 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -581,7 +581,7 @@ class NaTType(_NaT): Examples -------- - >>> pd.Timestamp.utcnow() + >>> pd.Timestamp.utcnow() # doctest: +SKIP Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """, ) @@ -705,7 +705,7 @@ class NaTType(_NaT): Examples -------- - >>> pd.Timestamp.now() + >>> pd.Timestamp.now() # doctest: +SKIP Timestamp('2020-11-16 22:06:16.378782') Analogous for ``pd.NaT``: @@ -730,7 +730,7 @@ class NaTType(_NaT): Examples -------- - >>> pd.Timestamp.today() + >>> pd.Timestamp.today() # doctest: +SKIP Timestamp('2020-11-16 22:37:39.969883') Analogous for ``pd.NaT``: diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index c53c8635c10e9..0998cb7b0c21e 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -2375,7 +2375,7 @@ cdef class _Period(PeriodMixin): >>> >>> a = Period(freq='D', year=2001, month=1, day=1) >>> a.strftime('%d-%b-%Y') - '01-Jan-2006' + '01-Jan-2001' >>> a.strftime('%b. %d, %Y was a %A') 'Jan. 01, 2001 was a Monday' """ diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 1094fac6d8504..613da5a691736 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -909,16 +909,20 @@ cdef class _Timestamp(ABCTimestamp): Examples -------- >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - >>> ts.to_period(freq='Y) # Year end frequency - numpy.datetime64('2020-03-14T15:32:52.192548651') + >>> # Year end frequency + >>> ts.to_period(freq='Y') + Period('2020', 'A-DEC') - >>> ts.to_period(freq='M') # Month end frequency + >>> # Month end frequency + >>> ts.to_period(freq='M') Period('2020-03', 'M') - >>> ts.to_period(freq='W') # Weekly frequency + >>> # Weekly frequency + >>> ts.to_period(freq='W') Period('2020-03-09/2020-03-15', 'W-SUN') - >>> ts.to_period(freq='Q') # Quarter end frequency + >>> # Quarter end frequency + >>> ts.to_period(freq='Q') Period('2020Q1', 'Q-DEC') """ from pandas import Period @@ -1059,7 +1063,7 @@ class Timestamp(_Timestamp): Examples -------- - >>> pd.Timestamp.now() + >>> pd.Timestamp.now() # doctest: +SKIP Timestamp('2020-11-16 22:06:16.378782') Analogous for ``pd.NaT``: @@ -1087,7 +1091,7 @@ class Timestamp(_Timestamp): Examples -------- - >>> pd.Timestamp.today() + >>> pd.Timestamp.today() # doctest: +SKIP Timestamp('2020-11-16 22:37:39.969883') Analogous for ``pd.NaT``: @@ -1106,7 +1110,7 @@ class Timestamp(_Timestamp): Examples -------- - >>> pd.Timestamp.utcnow() + >>> pd.Timestamp.utcnow() # doctest: +SKIP Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """ return cls.now(UTC) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 0809033b02934..60f90cc17ae34 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -385,25 +385,23 @@ def tz_standardize(tz: tzinfo) -> tzinfo: ------- tzinfo - Examples: + Examples -------- + >>> from datetime import datetime + >>> from pytz import timezone + >>> tz = timezone('US/Pacific').normalize( + ... datetime(2014, 1, 1, tzinfo=pytz.utc) + ... ).tzinfo >>> tz - >>> tz_standardize(tz) + >>> tz = timezone('US/Pacific') >>> tz - >>> tz_standardize(tz) - - >>> tz - dateutil.tz.tz.tzutc - - >>> tz_standardize(tz) - dateutil.tz.tz.tzutc """ if treat_tz_as_pytz(tz): return pytz.timezone(str(tz)) diff --git a/requirements-dev.txt b/requirements-dev.txt index 9b35de4bccb48..da2006a8b5c05 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -85,4 +85,5 @@ types-python-dateutil types-PyMySQL types-pytz types-setuptools +pytest-cython setuptools>=51.0.0