Skip to content

Commit 25d893b

Browse files
authored
DOC: Fixed examples in pandas/core/arrays/ (#33179)
1 parent b7fcae7 commit 25d893b

File tree

10 files changed

+188
-84
lines changed

10 files changed

+188
-84
lines changed

ci/code_checks.sh

+2-11
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,17 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
288288
RET=$(($RET + $?)) ; echo $MSG "DONE"
289289

290290
MSG='Doctests interval classes' ; echo $MSG
291-
pytest -q --doctest-modules \
292-
pandas/core/indexes/interval.py \
293-
pandas/core/arrays/interval.py
291+
pytest -q --doctest-modules pandas/core/indexes/interval.py
294292
RET=$(($RET + $?)) ; echo $MSG "DONE"
295293

296294
MSG='Doctests arrays'; echo $MSG
297-
pytest -q --doctest-modules \
298-
pandas/core/arrays/string_.py \
299-
pandas/core/arrays/integer.py \
300-
pandas/core/arrays/boolean.py
295+
pytest -q --doctest-modules pandas/core/arrays/
301296
RET=$(($RET + $?)) ; echo $MSG "DONE"
302297

303298
MSG='Doctests dtypes'; echo $MSG
304299
pytest -q --doctest-modules pandas/core/dtypes/
305300
RET=$(($RET + $?)) ; echo $MSG "DONE"
306301

307-
MSG='Doctests arrays/boolean.py' ; echo $MSG
308-
pytest -q --doctest-modules pandas/core/arrays/boolean.py
309-
RET=$(($RET + $?)) ; echo $MSG "DONE"
310-
311302
MSG='Doctests base.py' ; echo $MSG
312303
pytest -q --doctest-modules pandas/core/base.py
313304
RET=$(($RET + $?)) ; echo $MSG "DONE"

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ def _create_method(cls, op, coerce_to_dtype=True):
11611161
--------
11621162
Given an ExtensionArray subclass called MyExtensionArray, use
11631163
1164-
>>> __add__ = cls._create_method(operator.add)
1164+
__add__ = cls._create_method(operator.add)
11651165
11661166
in the class definition of MyExtensionArray to create the operator
11671167
for addition, that will be based on the operator implementation

pandas/core/arrays/categorical.py

+110-27
Original file line numberDiff line numberDiff line change
@@ -1598,19 +1598,19 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"):
15981598
15991599
>>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])
16001600
>>> c
1601-
[NaN, 2.0, 2.0, NaN, 5.0]
1601+
[NaN, 2, 2, NaN, 5]
16021602
Categories (2, int64): [2, 5]
16031603
>>> c.sort_values()
1604-
[2.0, 2.0, 5.0, NaN, NaN]
1604+
[2, 2, 5, NaN, NaN]
16051605
Categories (2, int64): [2, 5]
16061606
>>> c.sort_values(ascending=False)
1607-
[5.0, 2.0, 2.0, NaN, NaN]
1607+
[5, 2, 2, NaN, NaN]
16081608
Categories (2, int64): [2, 5]
16091609
>>> c.sort_values(na_position='first')
1610-
[NaN, NaN, 2.0, 2.0, 5.0]
1610+
[NaN, NaN, 2, 2, 5]
16111611
Categories (2, int64): [2, 5]
16121612
>>> c.sort_values(ascending=False, na_position='first')
1613-
[NaN, NaN, 5.0, 2.0, 2.0]
1613+
[NaN, NaN, 5, 2, 2]
16141614
Categories (2, int64): [2, 5]
16151615
"""
16161616
inplace = validate_bool_kwarg(inplace, "inplace")
@@ -1835,7 +1835,7 @@ def take(self, indexer, allow_fill: bool = False, fill_value=None):
18351835
18361836
>>> cat.take([0, -1, -1], allow_fill=True, fill_value='a')
18371837
[a, a, a]
1838-
Categories (3, object): [a, b]
1838+
Categories (2, object): [a, b]
18391839
18401840
Specifying a fill value that's not in ``self.categories``
18411841
will raise a ``TypeError``.
@@ -2231,33 +2231,32 @@ def unique(self):
22312231
-------
22322232
unique values : ``Categorical``
22332233
2234+
See Also
2235+
--------
2236+
pandas.unique
2237+
CategoricalIndex.unique
2238+
Series.unique
2239+
22342240
Examples
22352241
--------
22362242
An unordered Categorical will return categories in the
22372243
order of appearance.
22382244
2239-
>>> pd.Categorical(list('baabc'))
2245+
>>> pd.Categorical(list("baabc")).unique()
22402246
[b, a, c]
22412247
Categories (3, object): [b, a, c]
22422248
2243-
>>> pd.Categorical(list('baabc'), categories=list('abc'))
2249+
>>> pd.Categorical(list("baabc"), categories=list("abc")).unique()
22442250
[b, a, c]
22452251
Categories (3, object): [b, a, c]
22462252
22472253
An ordered Categorical preserves the category ordering.
22482254
2249-
>>> pd.Categorical(list('baabc'),
2250-
... categories=list('abc'),
2251-
... ordered=True)
2255+
>>> pd.Categorical(
2256+
... list("baabc"), categories=list("abc"), ordered=True
2257+
... ).unique()
22522258
[b, a, c]
22532259
Categories (3, object): [a < b < c]
2254-
2255-
See Also
2256-
--------
2257-
unique
2258-
CategoricalIndex.unique
2259-
Series.unique
2260-
22612260
"""
22622261
# unlike np.unique, unique1d does not sort
22632262
unique_codes = unique1d(self.codes)
@@ -2438,7 +2437,7 @@ def replace(self, to_replace, value, inplace: bool = False):
24382437
--------
24392438
>>> s = pd.Categorical([1, 2, 1, 3])
24402439
>>> s.replace(1, 3)
2441-
[3, 3, 2, 3]
2440+
[3, 2, 3, 3]
24422441
Categories (2, int64): [2, 3]
24432442
"""
24442443
inplace = validate_bool_kwarg(inplace, "inplace")
@@ -2506,16 +2505,100 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):
25062505
25072506
Examples
25082507
--------
2508+
>>> s = pd.Series(list("abbccc")).astype("category")
2509+
>>> s
2510+
0 a
2511+
1 b
2512+
2 b
2513+
3 c
2514+
4 c
2515+
5 c
2516+
dtype: category
2517+
Categories (3, object): [a, b, c]
2518+
25092519
>>> s.cat.categories
2510-
>>> s.cat.categories = list('abc')
2511-
>>> s.cat.rename_categories(list('cab'))
2512-
>>> s.cat.reorder_categories(list('cab'))
2513-
>>> s.cat.add_categories(['d','e'])
2514-
>>> s.cat.remove_categories(['d'])
2515-
>>> s.cat.remove_unused_categories()
2516-
>>> s.cat.set_categories(list('abcde'))
2520+
Index(['a', 'b', 'c'], dtype='object')
2521+
2522+
>>> s.cat.rename_categories(list("cba"))
2523+
0 c
2524+
1 b
2525+
2 b
2526+
3 a
2527+
4 a
2528+
5 a
2529+
dtype: category
2530+
Categories (3, object): [c, b, a]
2531+
2532+
>>> s.cat.reorder_categories(list("cba"))
2533+
0 a
2534+
1 b
2535+
2 b
2536+
3 c
2537+
4 c
2538+
5 c
2539+
dtype: category
2540+
Categories (3, object): [c, b, a]
2541+
2542+
>>> s.cat.add_categories(["d", "e"])
2543+
0 a
2544+
1 b
2545+
2 b
2546+
3 c
2547+
4 c
2548+
5 c
2549+
dtype: category
2550+
Categories (5, object): [a, b, c, d, e]
2551+
2552+
>>> s.cat.remove_categories(["a", "c"])
2553+
0 NaN
2554+
1 b
2555+
2 b
2556+
3 NaN
2557+
4 NaN
2558+
5 NaN
2559+
dtype: category
2560+
Categories (1, object): [b]
2561+
2562+
>>> s1 = s.cat.add_categories(["d", "e"])
2563+
>>> s1.cat.remove_unused_categories()
2564+
0 a
2565+
1 b
2566+
2 b
2567+
3 c
2568+
4 c
2569+
5 c
2570+
dtype: category
2571+
Categories (3, object): [a, b, c]
2572+
2573+
>>> s.cat.set_categories(list("abcde"))
2574+
0 a
2575+
1 b
2576+
2 b
2577+
3 c
2578+
4 c
2579+
5 c
2580+
dtype: category
2581+
Categories (5, object): [a, b, c, d, e]
2582+
25172583
>>> s.cat.as_ordered()
2584+
0 a
2585+
1 b
2586+
2 b
2587+
3 c
2588+
4 c
2589+
5 c
2590+
dtype: category
2591+
Categories (3, object): [a < b < c]
2592+
25182593
>>> s.cat.as_unordered()
2594+
0 a
2595+
1 b
2596+
2 b
2597+
3 c
2598+
4 c
2599+
5 c
2600+
dtype: category
2601+
Categories (3, object): [a, b, c]
25192602
"""
25202603

25212604
def __init__(self, data):
@@ -2603,7 +2686,7 @@ def _recode_for_categories(codes: np.ndarray, old_categories, new_categories):
26032686
>>> new_cat = pd.Index(['a', 'b'])
26042687
>>> codes = np.array([0, 1, 1, 2])
26052688
>>> _recode_for_categories(codes, old_cat, new_cat)
2606-
array([ 1, 0, 0, -1])
2689+
array([ 1, 0, 0, -1], dtype=int8)
26072690
"""
26082691
if len(old_categories) == 0:
26092692
# All null anyway, so just retain the nulls

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _unbox_scalar(self, value: Union[Period, Timestamp, Timedelta, NaTType]) ->
181181
182182
Examples
183183
--------
184-
>>> self._unbox_scalar(Timedelta('10s')) # DOCTEST: +SKIP
184+
>>> self._unbox_scalar(Timedelta("10s")) # doctest: +SKIP
185185
10000000000
186186
"""
187187
raise AbstractMethodError(self)

pandas/core/arrays/datetimes.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -922,9 +922,10 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
922922
... '2018-10-28 02:36:00',
923923
... '2018-10-28 03:46:00']))
924924
>>> s.dt.tz_localize('CET', ambiguous=np.array([True, True, False]))
925-
0 2015-03-29 03:00:00+02:00
926-
1 2015-03-29 03:30:00+02:00
927-
dtype: datetime64[ns, Europe/Warsaw]
925+
0 2018-10-28 01:20:00+02:00
926+
1 2018-10-28 02:36:00+02:00
927+
2 2018-10-28 03:46:00+01:00
928+
dtype: datetime64[ns, CET]
928929
929930
If the DST transition causes nonexistent times, you can shift these
930931
dates forward or backwards with a timedelta object or `'shift_forward'`
@@ -935,15 +936,17 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
935936
>>> s.dt.tz_localize('Europe/Warsaw', nonexistent='shift_forward')
936937
0 2015-03-29 03:00:00+02:00
937938
1 2015-03-29 03:30:00+02:00
938-
dtype: datetime64[ns, 'Europe/Warsaw']
939+
dtype: datetime64[ns, Europe/Warsaw]
940+
939941
>>> s.dt.tz_localize('Europe/Warsaw', nonexistent='shift_backward')
940942
0 2015-03-29 01:59:59.999999999+01:00
941943
1 2015-03-29 03:30:00+02:00
942-
dtype: datetime64[ns, 'Europe/Warsaw']
944+
dtype: datetime64[ns, Europe/Warsaw]
945+
943946
>>> s.dt.tz_localize('Europe/Warsaw', nonexistent=pd.Timedelta('1H'))
944947
0 2015-03-29 03:30:00+02:00
945948
1 2015-03-29 03:30:00+02:00
946-
dtype: datetime64[ns, 'Europe/Warsaw']
949+
dtype: datetime64[ns, Europe/Warsaw]
947950
"""
948951
nonexistent_options = ("raise", "NaT", "shift_forward", "shift_backward")
949952
if nonexistent not in nonexistent_options and not isinstance(
@@ -1604,9 +1607,9 @@ def date(self):
16041607
DatetimeIndex(['2012-12-31', '2013-12-31', '2014-12-31'],
16051608
dtype='datetime64[ns]', freq='A-DEC')
16061609
>>> idx.is_leap_year
1607-
array([ True, False, False], dtype=bool)
1610+
array([ True, False, False])
16081611
1609-
>>> dates = pd.Series(idx)
1612+
>>> dates_series = pd.Series(idx)
16101613
>>> dates_series
16111614
0 2012-12-31
16121615
1 2013-12-31

pandas/core/arrays/masked.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def to_numpy(
9494
9595
>>> a = pd.array([True, False, pd.NA], dtype="boolean")
9696
>>> a.to_numpy()
97-
array([True, False, NA], dtype=object)
97+
array([True, False, <NA>], dtype=object)
9898
9999
When no missing values are present, an equivalent dtype can be used.
100100
@@ -110,7 +110,7 @@ def to_numpy(
110110
>>> a = pd.array([True, False, pd.NA], dtype="boolean")
111111
>>> a
112112
<BooleanArray>
113-
[True, False, NA]
113+
[True, False, <NA>]
114114
Length: 3, dtype: boolean
115115
116116
>>> a.to_numpy(dtype="bool")

pandas/core/arrays/period.py

+1
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ def period_array(
818818
Integers that look like years are handled
819819
820820
>>> period_array([2000, 2001, 2002], freq='D')
821+
<PeriodArray>
821822
['2000-01-01', '2001-01-01', '2002-01-01']
822823
Length: 3, dtype: period[D]
823824

0 commit comments

Comments
 (0)