Pandas Series: dt.to_pytimedelta() function
Series-dt.to_pytimedelta() function
The dt.to_pytimedelta() function is used to return an array of native datetime.timedelta objects.
Python's standard datetime library uses a different representation timedelta’s. This method converts a Series of pandas Timedeltas to datetime.timedelta format with the same length as the original Series.
Syntax:
Series.dt.to_pytimedelta(self)
Returns: a : numpy.ndarray
Array of 1D containing data with datetime.timedelta type.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(pd.to_timedelta(np.arange(6), unit='d'))
s
Output:
0 0 days 1 1 days 2 2 days 3 3 days 4 4 days 5 5 days dtype: timedelta64[ns]
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(pd.to_timedelta(np.arange(6), unit='d'))
s.dt.to_pytimedelta()
Output:
array([datetime.timedelta(0), datetime.timedelta(days=1), datetime.timedelta(days=2), datetime.timedelta(days=3), datetime.timedelta(days=4), datetime.timedelta(days=5)], dtype=object)
Previous: Series-dt.components() function
Next: Series-dt.total_seconds() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-dt-to_pytimedelta.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics