Skip to content

TST: fixed cython doctests #43768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 5 additions & 2 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,7 +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)
>>> fac = Int64Factorizer(3)
>>> arr = np.array([1,2,np.nan]).astype(np.int64)
>>> fac.factorize(arr, na_sentinel=20) # doctest: +SKIP
array([ 0, 1, 20])
"""
cdef:
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 @@ -1071,11 +1073,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 @@ -1350,6 +1353,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
14 changes: 8 additions & 6 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -385,24 +385,26 @@ def tz_standardize(tz: tzinfo) -> tzinfo:
-------
tzinfo

Examples:
Examples
--------
>>> from datetime import datetime
>>> from pytz import timezone as tz
>>> tz = tz('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>

>>> from pytz import timezone
>>> 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
>>> tz # doctest: +SKIP
dateutil.tz.tz.tzutc

>>> tz_standardize(tz)
>>> tz_standardize(tz) # doctest: +SKIP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need skipping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to replicate this example

In [33]: from dateutil.tz.tz import tzutc as tz

In [34]: tz
Out[34]: dateutil.tz.tz.tzutc

In [35]: from pandas._libs.tslibs.timezones import tz_standardize

In [36]: tz_standardize(tz)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-94d2ff882b92> in <module>
----> 1 tz_standardize(tz)

TypeError: Argument 'tz' has incorrect type (expected datetime.tzinfo, got _TzSingleton)

Copy link
Member

@MarcoGorelli MarcoGorelli Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback sorry to unresolve this, but can this example be removed if it can't be run?

dateutil.tz.tz.tzutc
"""
if treat_tz_as_pytz(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