Skip to content

Commit

Permalink
TST: fixed cython doctests (pandas-dev#43768)
Browse files Browse the repository at this point in the history
  • Loading branch information
debnathshoham authored Sep 30, 2021
1 parent 9179081 commit 69059e8
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 28 deletions.
4 changes: 4 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ dependencies:
- types-PyMySQL
- types-pytz
- types-setuptools
- pytest-cython
8 changes: 5 additions & 3 deletions pandas/_libs/hashtable.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1355,6 +1358,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:

Examples
--------
>>> import datetime
>>> infer_dtype(['foo', 'bar'])
'string'

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Resolution(Enum):
Examples
--------
>>> Resolution.from_attrname('second')
2
<Resolution.RESO_SEC: 3>

>>> Resolution.from_attrname('second') == Resolution.RESO_SEC
True
Expand All @@ -244,7 +244,7 @@ class Resolution(Enum):
Examples
--------
>>> Resolution.get_reso_from_freq('H')
4
<Resolution.RESO_HR: 5>

>>> Resolution.get_reso_from_freq('H') == Resolution.RESO_HR
True
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
""",
)
Expand Down Expand Up @@ -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``:
Expand All @@ -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``:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
"""
Expand Down
20 changes: 12 additions & 8 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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``:
Expand Down Expand Up @@ -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``:
Expand All @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>

>>> tz_standardize(tz)
<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>

>>> tz = timezone('US/Pacific')
>>> tz
<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>

>>> tz_standardize(tz)
<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>

>>> tz
dateutil.tz.tz.tzutc

>>> tz_standardize(tz)
dateutil.tz.tz.tzutc
"""
if treat_tz_as_pytz(tz):
return pytz.timezone(str(tz))
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ types-python-dateutil
types-PyMySQL
types-pytz
types-setuptools
pytest-cython
setuptools>=51.0.0

0 comments on commit 69059e8

Please sign in to comment.