Skip to content

Commit c913db2

Browse files
authored
DOC: Fixing EX01 - Added examples (#53450)
Added examples
1 parent 4ff4e63 commit c913db2

File tree

5 files changed

+95
-13
lines changed

5 files changed

+95
-13
lines changed

ci/code_checks.sh

-12
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
258258
pandas.IntervalIndex.to_tuples \
259259
pandas.MultiIndex.dtypes \
260260
pandas.MultiIndex.drop \
261-
pandas.DatetimeIndex \
262-
pandas.DatetimeIndex.date \
263-
pandas.DatetimeIndex.time \
264-
pandas.DatetimeIndex.timetz \
265-
pandas.DatetimeIndex.dayofyear \
266-
pandas.DatetimeIndex.day_of_year \
267-
pandas.DatetimeIndex.quarter \
268-
pandas.DatetimeIndex.tz \
269-
pandas.DatetimeIndex.freqstr \
270-
pandas.DatetimeIndex.inferred_freq \
271-
pandas.DatetimeIndex.indexer_at_time \
272261
pandas.DatetimeIndex.indexer_between_time \
273262
pandas.DatetimeIndex.snap \
274263
pandas.DatetimeIndex.as_unit \
@@ -277,7 +266,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
277266
pandas.DatetimeIndex.mean \
278267
pandas.DatetimeIndex.std \
279268
pandas.TimedeltaIndex \
280-
pandas.TimedeltaIndex.days \
281269
pandas.TimedeltaIndex.seconds \
282270
pandas.TimedeltaIndex.microseconds \
283271
pandas.TimedeltaIndex.nanoseconds \

pandas/core/arrays/datetimelike.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,20 @@ def _maybe_mask_results(
825825
@property
826826
def freqstr(self) -> str | None:
827827
"""
828-
Return the frequency object as a string if its set, otherwise None.
828+
Return the frequency object as a string if it's set, otherwise None.
829+
830+
Examples
831+
--------
832+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00"], freq="D")
833+
>>> idx.freqstr
834+
'D'
835+
836+
The frequency can be inferred if there are more than 2 points:
837+
838+
>>> idx = pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"],
839+
... freq="infer")
840+
>>> idx.freqstr
841+
'2D'
829842
"""
830843
if self.freq is None:
831844
return None
@@ -837,6 +850,12 @@ def inferred_freq(self) -> str | None:
837850
Tries to return a string representing a frequency generated by infer_freq.
838851
839852
Returns None if it can't autodetect the frequency.
853+
854+
Examples
855+
--------
856+
>>> idx = pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"])
857+
>>> idx.inferred_freq
858+
'2D'
840859
"""
841860
if self.ndim != 1:
842861
return None

pandas/core/arrays/datetimes.py

+55
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ def tz(self) -> tzinfo | None:
567567
568568
Examples
569569
--------
570+
For Series:
571+
570572
>>> s = pd.Series(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
571573
>>> s = pd.to_datetime(s)
572574
>>> s
@@ -575,6 +577,13 @@ def tz(self) -> tzinfo | None:
575577
dtype: datetime64[ns, UTC]
576578
>>> s.dt.tz
577579
datetime.timezone.utc
580+
581+
For DatetimeIndex:
582+
583+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00",
584+
... "2/1/2020 11:00:00+00:00"])
585+
>>> idx.tz
586+
datetime.timezone.utc
578587
"""
579588
# GH 18595
580589
return getattr(self.dtype, "tz", None)
@@ -1326,6 +1335,8 @@ def time(self) -> npt.NDArray[np.object_]:
13261335
13271336
Examples
13281337
--------
1338+
For Series:
1339+
13291340
>>> s = pd.Series(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
13301341
>>> s = pd.to_datetime(s)
13311342
>>> s
@@ -1336,6 +1347,13 @@ def time(self) -> npt.NDArray[np.object_]:
13361347
0 10:00:00
13371348
1 11:00:00
13381349
dtype: object
1350+
1351+
For DatetimeIndex:
1352+
1353+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00",
1354+
... "2/1/2020 11:00:00+00:00"])
1355+
>>> idx.time
1356+
array([datetime.time(10, 0), datetime.time(11, 0)], dtype=object)
13391357
"""
13401358
# If the Timestamps have a timezone that is not UTC,
13411359
# convert them into their i8 representation while
@@ -1353,6 +1371,8 @@ def timetz(self) -> npt.NDArray[np.object_]:
13531371
13541372
Examples
13551373
--------
1374+
For Series:
1375+
13561376
>>> s = pd.Series(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
13571377
>>> s = pd.to_datetime(s)
13581378
>>> s
@@ -1363,6 +1383,14 @@ def timetz(self) -> npt.NDArray[np.object_]:
13631383
0 10:00:00+00:00
13641384
1 11:00:00+00:00
13651385
dtype: object
1386+
1387+
For DatetimeIndex:
1388+
1389+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00",
1390+
... "2/1/2020 11:00:00+00:00"])
1391+
>>> idx.timetz
1392+
array([datetime.time(10, 0, tzinfo=datetime.timezone.utc),
1393+
datetime.time(11, 0, tzinfo=datetime.timezone.utc)], dtype=object)
13661394
"""
13671395
return ints_to_pydatetime(self.asi8, self.tz, box="time", reso=self._creso)
13681396

@@ -1376,6 +1404,8 @@ def date(self) -> npt.NDArray[np.object_]:
13761404
13771405
Examples
13781406
--------
1407+
For Series:
1408+
13791409
>>> s = pd.Series(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
13801410
>>> s = pd.to_datetime(s)
13811411
>>> s
@@ -1386,6 +1416,13 @@ def date(self) -> npt.NDArray[np.object_]:
13861416
0 2020-01-01
13871417
1 2020-02-01
13881418
dtype: object
1419+
1420+
For DatetimeIndex:
1421+
1422+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00",
1423+
... "2/1/2020 11:00:00+00:00"])
1424+
>>> idx.date
1425+
array([datetime.date(2020, 1, 1), datetime.date(2020, 2, 1)], dtype=object)
13891426
"""
13901427
# If the Timestamps have a timezone that is not UTC,
13911428
# convert them into their i8 representation while
@@ -1667,6 +1704,8 @@ def isocalendar(self) -> DataFrame:
16671704
16681705
Examples
16691706
--------
1707+
For Series:
1708+
16701709
>>> s = pd.Series(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
16711710
>>> s = pd.to_datetime(s)
16721711
>>> s
@@ -1677,6 +1716,13 @@ def isocalendar(self) -> DataFrame:
16771716
0 1
16781717
1 32
16791718
dtype: int32
1719+
1720+
For DatetimeIndex:
1721+
1722+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00",
1723+
... "2/1/2020 11:00:00+00:00"])
1724+
>>> idx.dayofyear
1725+
Index([1, 32], dtype='int32')
16801726
""",
16811727
)
16821728
dayofyear = day_of_year
@@ -1688,6 +1734,8 @@ def isocalendar(self) -> DataFrame:
16881734
16891735
Examples
16901736
--------
1737+
For Series:
1738+
16911739
>>> s = pd.Series(["1/1/2020 10:00:00+00:00", "4/1/2020 11:00:00+00:00"])
16921740
>>> s = pd.to_datetime(s)
16931741
>>> s
@@ -1698,6 +1746,13 @@ def isocalendar(self) -> DataFrame:
16981746
0 1
16991747
1 2
17001748
dtype: int32
1749+
1750+
For DatetimeIndex:
1751+
1752+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00",
1753+
... "2/1/2020 11:00:00+00:00"])
1754+
>>> idx.quarter
1755+
Index([1, 1], dtype='int32')
17011756
""",
17021757
)
17031758
days_in_month = _field_accessor(

pandas/core/arrays/period.py

+6
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ def __arrow_array__(self, type=None):
431431
"day",
432432
"""
433433
The days of the period.
434+
435+
Examples
436+
--------
437+
>>> idx = pd.PeriodIndex(['2020-01-31', '2020-02-28'], freq='D')
438+
>>> idx.day
439+
Index([31, 28], dtype='int64')
434440
""",
435441
)
436442
hour = _field_accessor(

pandas/core/indexes/datetimes.py

+14
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
248248
-----
249249
To learn more about the frequency strings, please see `this link
250250
<https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`__.
251+
252+
Examples
253+
--------
254+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
255+
>>> idx
256+
DatetimeIndex(['2020-01-01 10:00:00+00:00', '2020-02-01 11:00:00+00:00'],
257+
dtype='datetime64[ns, UTC]', freq=None)
251258
"""
252259

253260
_typ = "datetimeindex"
@@ -709,6 +716,13 @@ def indexer_at_time(self, time, asof: bool = False) -> npt.NDArray[np.intp]:
709716
indexer_between_time : Get index locations of values between particular
710717
times of day.
711718
DataFrame.at_time : Select values at particular time of day.
719+
720+
Examples
721+
--------
722+
>>> idx = pd.DatetimeIndex(["1/1/2020 10:00", "2/1/2020 11:00",
723+
... "3/1/2020 10:00"])
724+
>>> idx.indexer_at_time("10:00")
725+
array([0, 2])
712726
"""
713727
if asof:
714728
raise NotImplementedError("'asof' argument is not supported")

0 commit comments

Comments
 (0)