Skip to content
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

DOC: Fixing EX01 - Added examples #53647

Merged
merged 7 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 0 additions & 11 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Timestamp.utcoffset \
pandas.Timestamp.utctimetuple \
pandas.Timestamp.weekday \
pandas.arrays.DatetimeArray \
pandas.Timedelta.view \
pandas.Timedelta.as_unit \
pandas.Timedelta.ceil \
pandas.Timedelta.floor \
pandas.Timedelta.round \
pandas.Timedelta.to_pytimedelta \
pandas.Timedelta.to_timedelta64 \
pandas.Timedelta.to_numpy \
pandas.Timedelta.total_seconds \
pandas.arrays.TimedeltaArray \
pandas.Period.asfreq \
pandas.Period.now \
Expand Down Expand Up @@ -261,7 +251,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.window.ewm.ExponentialMovingWindow.cov \
pandas.api.indexers.BaseIndexer \
pandas.api.indexers.VariableOffsetWindowIndexer \
pandas.core.groupby.SeriesGroupBy.fillna \
pandas.io.formats.style.Styler \
pandas.io.formats.style.Styler.from_custom_template \
pandas.io.formats.style.Styler.set_caption \
Expand Down
76 changes: 75 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,17 @@ cdef class _Timedelta(timedelta):
return self._ms * 1000 + self._us

def total_seconds(self) -> float:
"""Total seconds in the duration."""
"""
Total seconds in the duration.

Examples
--------
>>> td = pd.Timedelta('1min')
>>> td
Timedelta('0 days 00:01:00')
>>> td.total_seconds()
60.0
"""
# We need to override bc we overrode days/seconds/microseconds
# TODO: add nanos/1e9?
return self.days * 24 * 3600 + self.seconds + self.microseconds / 1_000_000
Expand Down Expand Up @@ -1274,6 +1284,14 @@ cdef class _Timedelta(timedelta):
Notes
-----
Any nanosecond resolution will be lost.

Examples
--------
>>> td = pd.Timedelta('3D')
>>> td
Timedelta('3 days 00:00:00')
>>> td.to_pytimedelta()
datetime.timedelta(days=3)
"""
if self._creso == NPY_FR_ns:
return timedelta(microseconds=int(self._value) / 1000)
Expand All @@ -1287,6 +1305,14 @@ cdef class _Timedelta(timedelta):
def to_timedelta64(self) -> np.timedelta64:
"""
Return a numpy.timedelta64 object with 'ns' precision.

Examples
--------
>>> td = pd.Timedelta('3D')
>>> td
Timedelta('3 days 00:00:00')
>>> td.to_timedelta64()
numpy.timedelta64(259200000000000,'ns')
"""
cdef:
str abbrev = npy_unit_to_abbrev(self._creso)
Expand All @@ -1309,6 +1335,14 @@ cdef class _Timedelta(timedelta):
See Also
--------
Series.to_numpy : Similar method for Series.

Examples
--------
>>> td = pd.Timedelta('3D')
>>> td
Timedelta('3 days 00:00:00')
>>> td.to_numpy()
numpy.timedelta64(259200000000000,'ns')
"""
if dtype is not None or copy is not False:
raise ValueError(
Expand All @@ -1324,6 +1358,14 @@ cdef class _Timedelta(timedelta):
----------
dtype : str or dtype
The dtype to view the underlying data as.

Examples
--------
>>> td = pd.Timedelta('3D')
>>> td
Timedelta('3 days 00:00:00')
>>> td.view(int)
259200000000000
"""
return np.timedelta64(self._value).view(dtype)

Expand Down Expand Up @@ -1603,6 +1645,14 @@ cdef class _Timedelta(timedelta):
Returns
-------
Timedelta

Examples
--------
>>> td = pd.Timedelta('1001ms')
>>> td
Timedelta('0 days 00:00:01.001000')
>>> td.as_unit('s')
Timedelta('0 days 00:00:01')
"""
dtype = np.dtype(f"m8[{unit}]")
reso = get_unit_from_dtype(dtype)
Expand Down Expand Up @@ -1875,6 +1925,14 @@ class Timedelta(_Timedelta):
Raises
------
ValueError if the freq cannot be converted

Examples
--------
>>> td = pd.Timedelta('1001ms')
>>> td
Timedelta('0 days 00:00:01.001000')
>>> td.round('s')
Timedelta('0 days 00:00:01')
"""
return self._round(freq, RoundTo.NEAREST_HALF_EVEN)

Expand All @@ -1886,6 +1944,14 @@ class Timedelta(_Timedelta):
----------
freq : str
Frequency string indicating the flooring resolution.

Examples
--------
>>> td = pd.Timedelta('1001ms')
>>> td
Timedelta('0 days 00:00:01.001000')
>>> td.floor('s')
Timedelta('0 days 00:00:01')
"""
return self._round(freq, RoundTo.MINUS_INFTY)

Expand All @@ -1897,6 +1963,14 @@ class Timedelta(_Timedelta):
----------
freq : str
Frequency string indicating the ceiling resolution.

Examples
--------
>>> td = pd.Timedelta('1001ms')
>>> td
Timedelta('0 days 00:00:01.001000')
>>> td.ceil('s')
Timedelta('0 days 00:00:02')
"""
return self._round(freq, RoundTo.PLUS_INFTY)

Expand Down
8 changes: 8 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): # type: ignore[misc]
Methods
-------
None

Examples
--------
>>> pd.arrays.DatetimeArray(pd.DatetimeIndex(['2023-01-01', '2023-01-02']),
... freq='D')
<DatetimeArray>
['2023-01-01 00:00:00', '2023-01-02 00:00:00']
Length: 2, dtype: datetime64[ns]
"""

_typ = "datetimearray"
Expand Down
19 changes: 19 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,25 @@ def fillna(
--------
ffill : Forward fill values within a group.
bfill : Backward fill values within a group.

Examples
--------
For SeriesGroupBy:

>>> lst = ['a', 'a', 'b', 'b']
>>> ser = pd.Series([1, np.nan, 3, np.nan], index=lst)
>>> ser
a 1.0
a NaN
b 3.0
b NaN
dtype: float64
>>> ser.groupby(level=0).fillna(value=0)
Copy link
Member

Choose a reason for hiding this comment

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

this one works, but isn't how one would usually use groupby.fillna - for this example, you could just do ser.fillna(0)

how about

ser.groupby(level=0).fillna(method='ffill')

to better show what's unique about this method?

Copy link
Member Author

@DeaMariaLeon DeaMariaLeon Jun 14, 2023

Choose a reason for hiding this comment

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

I thought that the keyword method there was deprecated #53496
But thanks, I'll write another example.

Copy link
Member

Choose a reason for hiding this comment

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

ah you're right, thanks!

a 1.0
a 0.0
b 3.0
b 0.0
dtype: float64
"""
result = self._op_via_apply(
"fillna",
Expand Down