Skip to content

Commit 1f860f3

Browse files
DOC: fixing GL08 errors for pandas.DatetimeIndex.as_unit and pandas.DatetimeIndex.freq (#57562)
* fixing PR01 errors for pandas.DatetimeIndex.as_unit and pandas.DatetimeIndex.freq * clean up pandas.DatetimeIndex.freq docstring validation errors * update link to use :ref: format * improving docstring for as_unit * add missing period * add missing period * Specify ValueError Co-authored-by: Jonas Bergner <[email protected]> * cleaning up --------- Co-authored-by: Jonas Bergner <[email protected]>
1 parent a6877d8 commit 1f860f3

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

ci/code_checks.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
7171

7272
MSG='Partially validate docstrings (PR02)' ; echo $MSG
7373
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=PR02 --ignore_functions \
74+
pandas.Series.dt.as_unit\
7475
pandas.Series.dt.to_period\
7576
pandas.Series.dt.tz_localize\
7677
pandas.Series.dt.tz_convert\
@@ -143,8 +144,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
143144

144145
MSG='Partially validate docstrings (GL08)' ; echo $MSG
145146
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=GL08 --ignore_functions \
146-
pandas.DatetimeIndex.as_unit\
147-
pandas.DatetimeIndex.freq\
148147
pandas.ExcelFile.book\
149148
pandas.ExcelFile.sheet_names\
150149
pandas.Index.empty\
@@ -454,6 +453,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
454453
pandas.Series.cat.rename_categories\
455454
pandas.Series.cat.reorder_categories\
456455
pandas.Series.cat.set_categories\
456+
pandas.Series.dt.as_unit\
457457
pandas.Series.dt.ceil\
458458
pandas.Series.dt.day_name\
459459
pandas.Series.dt.floor\

pandas/core/arrays/datetimelike.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,29 @@ def _validate_dtype(cls, values, dtype):
20412041
def freq(self):
20422042
"""
20432043
Return the frequency object if it is set, otherwise None.
2044+
2045+
To learn more about the frequency strings, please see
2046+
:ref:`this link<timeseries.offset_aliases>`.
2047+
2048+
See Also
2049+
--------
2050+
DatetimeIndex.freq : Return the frequency object if it is set, otherwise None.
2051+
PeriodIndex.freq : Return the frequency object if it is set, otherwise None.
2052+
2053+
Examples
2054+
--------
2055+
>>> datetimeindex = pd.date_range(
2056+
... "2022-02-22 02:22:22", periods=10, tz="America/Chicago", freq="h"
2057+
... )
2058+
>>> datetimeindex
2059+
DatetimeIndex(['2022-02-22 02:22:22-06:00', '2022-02-22 03:22:22-06:00',
2060+
'2022-02-22 04:22:22-06:00', '2022-02-22 05:22:22-06:00',
2061+
'2022-02-22 06:22:22-06:00', '2022-02-22 07:22:22-06:00',
2062+
'2022-02-22 08:22:22-06:00', '2022-02-22 09:22:22-06:00',
2063+
'2022-02-22 10:22:22-06:00', '2022-02-22 11:22:22-06:00'],
2064+
dtype='datetime64[ns, America/Chicago]', freq='h')
2065+
>>> datetimeindex.freq
2066+
<Hour>
20442067
"""
20452068
return self._freq
20462069

@@ -2154,6 +2177,47 @@ def unit(self) -> str:
21542177
return dtype_to_unit(self.dtype) # type: ignore[arg-type]
21552178

21562179
def as_unit(self, unit: str, round_ok: bool = True) -> Self:
2180+
"""
2181+
Convert to a dtype with the given unit resolution.
2182+
2183+
The limits of timestamp representation depend on the chosen resolution.
2184+
Different resolutions can be converted to each other through as_unit.
2185+
2186+
Parameters
2187+
----------
2188+
unit : {'s', 'ms', 'us', 'ns'}
2189+
round_ok : bool, default True
2190+
If False and the conversion requires rounding, raise ValueError.
2191+
2192+
Returns
2193+
-------
2194+
same type as self
2195+
Converted to the specified unit.
2196+
2197+
See Also
2198+
--------
2199+
Timestamp.as_unit : Convert to the given unit.
2200+
2201+
Examples
2202+
--------
2203+
For :class:`pandas.DatetimeIndex`:
2204+
2205+
>>> idx = pd.DatetimeIndex(["2020-01-02 01:02:03.004005006"])
2206+
>>> idx
2207+
DatetimeIndex(['2020-01-02 01:02:03.004005006'],
2208+
dtype='datetime64[ns]', freq=None)
2209+
>>> idx.as_unit("s")
2210+
DatetimeIndex(['2020-01-02 01:02:03'], dtype='datetime64[s]', freq=None)
2211+
2212+
For :class:`pandas.TimedeltaIndex`:
2213+
2214+
>>> tdelta_idx = pd.to_timedelta(["1 day 3 min 2 us 42 ns"])
2215+
>>> tdelta_idx
2216+
TimedeltaIndex(['1 days 00:03:00.000002042'],
2217+
dtype='timedelta64[ns]', freq=None)
2218+
>>> tdelta_idx.as_unit("s")
2219+
TimedeltaIndex(['1 days 00:03:00'], dtype='timedelta64[s]', freq=None)
2220+
"""
21572221
if unit not in ["s", "ms", "us", "ns"]:
21582222
raise ValueError("Supported units are 's', 'ms', 'us', 'ns'")
21592223

pandas/core/indexes/datetimelike.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ def mean(self, *, skipna: bool = True, axis: int | None = 0):
9494

9595
@property
9696
def freq(self) -> BaseOffset | None:
97+
"""
98+
Return the frequency object if it is set, otherwise None.
99+
100+
To learn more about the frequency strings, please see
101+
:ref:`this link<timeseries.offset_aliases>`.
102+
103+
See Also
104+
--------
105+
DatetimeIndex.freq : Return the frequency object if it is set, otherwise None.
106+
PeriodIndex.freq : Return the frequency object if it is set, otherwise None.
107+
108+
Examples
109+
--------
110+
>>> datetimeindex = pd.date_range(
111+
... "2022-02-22 02:22:22", periods=10, tz="America/Chicago", freq="h"
112+
... )
113+
>>> datetimeindex
114+
DatetimeIndex(['2022-02-22 02:22:22-06:00', '2022-02-22 03:22:22-06:00',
115+
'2022-02-22 04:22:22-06:00', '2022-02-22 05:22:22-06:00',
116+
'2022-02-22 06:22:22-06:00', '2022-02-22 07:22:22-06:00',
117+
'2022-02-22 08:22:22-06:00', '2022-02-22 09:22:22-06:00',
118+
'2022-02-22 10:22:22-06:00', '2022-02-22 11:22:22-06:00'],
119+
dtype='datetime64[ns, America/Chicago]', freq='h')
120+
>>> datetimeindex.freq
121+
<Hour>
122+
"""
97123
return self._data.freq
98124

99125
@freq.setter

0 commit comments

Comments
 (0)