- Python:Data Analytics and Visualization
- Phuong Vo.T.H Martin Czygan Ashish Kumar Kirthi Raman
- 307字
- 2021-07-09 18:51:43
Timedeltas
Along with the powerful timestamp object, which acts as a building block for the DatetimeIndex
, there is another useful data structure, which has been introduced in Pandas 0.15 – the Timedelta. The Timedelta can serve as a basis for indices as well, in this case a TimedeltaIndex
.
Timedeltas are differences in times, expressed in difference units. The Timedelta
class in Pandas is a subclass of datetime.timedelta
from the Python standard library. As with other Pandas data structures, the Timedelta can be constructed from a variety of inputs:
>>> pd.Timedelta('1 days') Timedelta('1 days 00:00:00') >>> pd.Timedelta('-1 days 2 min 10s 3us') Timedelta('-2 days +23:57:49.999997') >>> pd.Timedelta(days=1,seconds=1) Timedelta('1 days 00:00:01')
As you would expect, Timedeltas
allow basic arithmetic:
>>> pd.Timedelta(days=1) + pd.Timedelta(seconds=1) Timedelta('1 days 00:00:01')
Similar to to_datetime
, there is a to_timedelta
function that can parse strings or lists of strings into Timedelta structures or TimedeltaIndices
:
>>> pd.to_timedelta('20.1s') Timedelta('0 days 00:00:20.100000')
Instead of absolute dates, we could create an index of timedeltas
. Imagine measurements from a volcano, for example. We might want to take measurements but index it from a given date, for example the date of the last eruption. We could create a timedelta
index that has the last seven days as entries:
>>> pd.to_timedelta(np.arange(7), unit='D') TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days', '5 days', '6 days'], dtype='timedelta64[ns]', freq=None)
We could then work with time series data, indexed from the last eruption. If we had measurements for many eruptions (from possibly multiple volcanos), we would have an index that would make comparisons and analysis of this data easier. For example, we could ask whether there is a typical pattern that occurs between the third day and the fifth day after an eruption. This question would not be impossible to answer with a DatetimeIndex
, but a TimedeltaIndex
makes this kind of exploration much more convenient.
- Windows 8應用開發實戰
- SharePoint 2010開發最佳實踐
- 可編程序控制器應用實訓(三菱機型)
- Nginx高性能Web服務器詳解
- Azure PowerShell Quick Start Guide
- Hadoop應用開發基礎
- Photoshop行業應用基礎
- Bayesian Analysis with Python
- Visual C++項目開發案例精粹
- 軟件構件技術
- Learning ServiceNow
- 水晶石影視動畫精粹:After Effects & Nuke 影視后期合成
- INSTANT VMware vCloud Starter
- Mastering Exploratory Analysis with pandas
- 網絡脆弱性掃描產品原理及應用