@@ -796,6 +796,16 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
796
796
Returns
797
797
-------
798
798
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)
799
809
"""
800
810
return ints_to_pytimedelta (self ._ndarray )
801
811
@@ -804,6 +814,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
804
814
805
815
Examples
806
816
--------
817
+ For Series:
818
+
807
819
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='d'))
808
820
>>> ser
809
821
0 1 days
@@ -814,7 +826,16 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
814
826
0 1
815
827
1 2
816
828
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')"""
818
839
)
819
840
days = _field_accessor ("days" , "days" , days_docstring )
820
841
@@ -823,6 +844,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
823
844
824
845
Examples
825
846
--------
847
+ For Series:
848
+
826
849
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='S'))
827
850
>>> ser
828
851
0 0 days 00:00:01
@@ -833,7 +856,16 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
833
856
0 1
834
857
1 2
835
858
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')"""
837
869
)
838
870
seconds = _field_accessor (
839
871
"seconds" ,
@@ -846,6 +878,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
846
878
847
879
Examples
848
880
--------
881
+ For Series:
882
+
849
883
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='U'))
850
884
>>> ser
851
885
0 0 days 00:00:00.000001
@@ -856,7 +890,17 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
856
890
0 1
857
891
1 2
858
892
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')"""
860
904
)
861
905
microseconds = _field_accessor (
862
906
"microseconds" ,
@@ -869,6 +913,8 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
869
913
870
914
Examples
871
915
--------
916
+ For Series:
917
+
872
918
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='N'))
873
919
>>> ser
874
920
0 0 days 00:00:00.000000001
@@ -879,7 +925,17 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
879
925
0 1
880
926
1 2
881
927
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')"""
883
939
)
884
940
nanoseconds = _field_accessor (
885
941
"nanoseconds" ,
@@ -898,6 +954,16 @@ def components(self) -> DataFrame:
898
954
Returns
899
955
-------
900
956
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
901
967
"""
902
968
from pandas import DataFrame
903
969
0 commit comments