Skip to content

Commit 32cf7f0

Browse files
DeaMariaLeonim-vinicius
authored and
im-vinicius
committed
DOC: Fixing EX01 - Added more examples (pandas-dev#53528)
More examples
1 parent c42a2fb commit 32cf7f0

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

ci/code_checks.sh

-8
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
241241
pandas.DatetimeIndex.mean \
242242
pandas.DatetimeIndex.std \
243243
pandas.TimedeltaIndex \
244-
pandas.TimedeltaIndex.seconds \
245-
pandas.TimedeltaIndex.microseconds \
246-
pandas.TimedeltaIndex.nanoseconds \
247-
pandas.TimedeltaIndex.components \
248-
pandas.TimedeltaIndex.inferred_freq \
249-
pandas.TimedeltaIndex.as_unit \
250-
pandas.TimedeltaIndex.to_pytimedelta \
251-
pandas.TimedeltaIndex.mean \
252244
pandas.core.window.rolling.Rolling.max \
253245
pandas.core.window.rolling.Rolling.cov \
254246
pandas.core.window.rolling.Rolling.skew \

pandas/core/arrays/datetimelike.py

+20
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,20 @@ def inferred_freq(self) -> str | None:
861861
862862
Examples
863863
--------
864+
For DatetimeIndex:
865+
864866
>>> idx = pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"])
865867
>>> idx.inferred_freq
866868
'2D'
869+
870+
For TimedeltaIndex:
871+
872+
>>> tdelta_idx = pd.to_timedelta(["0 days", "10 days", "20 days"])
873+
>>> tdelta_idx
874+
TimedeltaIndex(['0 days', '10 days', '20 days'],
875+
dtype='timedelta64[ns]', freq=None)
876+
>>> tdelta_idx.inferred_freq
877+
'10D'
867878
"""
868879
if self.ndim != 1:
869880
return None
@@ -1535,6 +1546,15 @@ def mean(self, *, skipna: bool = True, axis: AxisInt | None = 0):
15351546
Notes
15361547
-----
15371548
mean is only defined for Datetime and Timedelta dtypes, not for Period.
1549+
1550+
Examples
1551+
--------
1552+
>>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit='D')
1553+
>>> tdelta_idx
1554+
TimedeltaIndex(['1 days', '2 days', '3 days'],
1555+
dtype='timedelta64[ns]', freq=None)
1556+
>>> tdelta_idx.mean()
1557+
Timedelta('2 days 00:00:00')
15381558
"""
15391559
if isinstance(self.dtype, PeriodDtype):
15401560
# See discussion in GH#24757

pandas/core/arrays/timedeltas.py

+70-4
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
796796
Returns
797797
-------
798798
numpy.ndarray
799+
800+
Examples
801+
--------
802+
>>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit='D')
803+
>>> tdelta_idx
804+
TimedeltaIndex(['1 days', '2 days', '3 days'],
805+
dtype='timedelta64[ns]', freq=None)
806+
>>> tdelta_idx.to_pytimedelta()
807+
array([datetime.timedelta(days=1), datetime.timedelta(days=2),
808+
datetime.timedelta(days=3)], dtype=object)
799809
"""
800810
return ints_to_pytimedelta(self._ndarray)
801811

@@ -804,6 +814,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
804814
805815
Examples
806816
--------
817+
For Series:
818+
807819
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='d'))
808820
>>> ser
809821
0 1 days
@@ -814,7 +826,16 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
814826
0 1
815827
1 2
816828
2 3
817-
dtype: int64"""
829+
dtype: int64
830+
831+
For TimedeltaIndex:
832+
833+
>>> tdelta_idx = pd.to_timedelta(["0 days", "10 days", "20 days"])
834+
>>> tdelta_idx
835+
TimedeltaIndex(['0 days', '10 days', '20 days'],
836+
dtype='timedelta64[ns]', freq=None)
837+
>>> tdelta_idx.days
838+
Index([0, 10, 20], dtype='int64')"""
818839
)
819840
days = _field_accessor("days", "days", days_docstring)
820841

@@ -823,6 +844,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
823844
824845
Examples
825846
--------
847+
For Series:
848+
826849
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='S'))
827850
>>> ser
828851
0 0 days 00:00:01
@@ -833,7 +856,16 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
833856
0 1
834857
1 2
835858
2 3
836-
dtype: int32"""
859+
dtype: int32
860+
861+
For TimedeltaIndex:
862+
863+
>>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit='S')
864+
>>> tdelta_idx
865+
TimedeltaIndex(['0 days 00:00:01', '0 days 00:00:02', '0 days 00:00:03'],
866+
dtype='timedelta64[ns]', freq=None)
867+
>>> tdelta_idx.seconds
868+
Index([1, 2, 3], dtype='int32')"""
837869
)
838870
seconds = _field_accessor(
839871
"seconds",
@@ -846,6 +878,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
846878
847879
Examples
848880
--------
881+
For Series:
882+
849883
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='U'))
850884
>>> ser
851885
0 0 days 00:00:00.000001
@@ -856,7 +890,17 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
856890
0 1
857891
1 2
858892
2 3
859-
dtype: int32"""
893+
dtype: int32
894+
895+
For TimedeltaIndex:
896+
897+
>>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit='U')
898+
>>> tdelta_idx
899+
TimedeltaIndex(['0 days 00:00:00.000001', '0 days 00:00:00.000002',
900+
'0 days 00:00:00.000003'],
901+
dtype='timedelta64[ns]', freq=None)
902+
>>> tdelta_idx.microseconds
903+
Index([1, 2, 3], dtype='int32')"""
860904
)
861905
microseconds = _field_accessor(
862906
"microseconds",
@@ -869,6 +913,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
869913
870914
Examples
871915
--------
916+
For Series:
917+
872918
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='N'))
873919
>>> ser
874920
0 0 days 00:00:00.000000001
@@ -879,7 +925,17 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
879925
0 1
880926
1 2
881927
2 3
882-
dtype: int32"""
928+
dtype: int32
929+
930+
For TimedeltaIndex:
931+
932+
>>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit='N')
933+
>>> tdelta_idx
934+
TimedeltaIndex(['0 days 00:00:00.000000001', '0 days 00:00:00.000000002',
935+
'0 days 00:00:00.000000003'],
936+
dtype='timedelta64[ns]', freq=None)
937+
>>> tdelta_idx.nanoseconds
938+
Index([1, 2, 3], dtype='int32')"""
883939
)
884940
nanoseconds = _field_accessor(
885941
"nanoseconds",
@@ -898,6 +954,16 @@ def components(self) -> DataFrame:
898954
Returns
899955
-------
900956
DataFrame
957+
958+
Examples
959+
--------
960+
>>> tdelta_idx = pd.to_timedelta(['1 day 3 min 2 us 42 ns'])
961+
>>> tdelta_idx
962+
TimedeltaIndex(['1 days 00:03:00.000002042'],
963+
dtype='timedelta64[ns]', freq=None)
964+
>>> tdelta_idx.components
965+
days hours minutes seconds milliseconds microseconds nanoseconds
966+
0 1 0 3 0 0 2 42
901967
"""
902968
from pandas import DataFrame
903969

pandas/core/indexes/datetimelike.py

+9
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ def as_unit(self, unit: str) -> Self:
431431
Returns
432432
-------
433433
same type as self
434+
435+
Examples
436+
--------
437+
>>> tdelta_idx = pd.to_timedelta(['1 day 3 min 2 us 42 ns'])
438+
>>> tdelta_idx
439+
TimedeltaIndex(['1 days 00:03:00.000002042'],
440+
dtype='timedelta64[ns]', freq=None)
441+
>>> tdelta_idx.as_unit('s')
442+
TimedeltaIndex(['1 days 00:03:00'], dtype='timedelta64[s]', freq=None)
434443
"""
435444
arr = self._data.as_unit(unit)
436445
return type(self)._simple_new(arr, name=self.name)

0 commit comments

Comments
 (0)