|
| 1 | + |
| 2 | +Python Pandas - Timedelta |
| 3 | + |
| 4 | + |
| 5 | +Timedeltas are differences in times, expressed in difference units, for example, days, hours, minutes, seconds. They can be both positive and negative. |
| 6 | +We can create Timedelta objects using various arguments as shown below − |
| 7 | +String |
| 8 | +By passing a string literal, we can create a timedelta object. |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +print pd.Timedelta('2 days 2 hours 15 minutes 30 seconds') |
| 12 | +Its output is as follows − |
| 13 | +2 days 02:15:30 |
| 14 | +Integer |
| 15 | +By passing an integer value with the unit, an argument creates a Timedelta object. |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | +print pd.Timedelta(6,unit='h') |
| 19 | +Its output is as follows − |
| 20 | +0 days 06:00:00 |
| 21 | +Data Offsets |
| 22 | +Data offsets such as - weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds can also be used in construction. |
| 23 | +import pandas as pd |
| 24 | + |
| 25 | +print pd.Timedelta(days=2) |
| 26 | +Its output is as follows − |
| 27 | +2 days 00:00:00 |
| 28 | +to_timedelta() |
| 29 | +Using the top-level pd.to_timedelta, you can convert a scalar, array, list, or series from a recognized timedelta format/ value into a Timedelta type. It will construct Series if the input is a Series, a scalar if the input is scalar-like, otherwise will output a TimedeltaIndex. |
| 30 | +import pandas as pd |
| 31 | + |
| 32 | +print pd.Timedelta(days=2) |
| 33 | +Its output is as follows − |
| 34 | +2 days 00:00:00 |
| 35 | +Operations |
| 36 | +You can operate on Series/ DataFrames and construct timedelta64[ns] Series through subtraction operations on datetime64[ns] Series, or Timestamps. |
| 37 | +Let us now create a DataFrame with Timedelta and datetime objects and perform some arithmetic operations on it − |
| 38 | +import pandas as pd |
| 39 | + |
| 40 | +s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D')) |
| 41 | +td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ]) |
| 42 | +df = pd.DataFrame(dict(A = s, B = td)) |
| 43 | +print df |
| 44 | +Its output is as follows − |
| 45 | + A B |
| 46 | +0 2012-01-01 0 days |
| 47 | +1 2012-01-02 1 days |
| 48 | +2 2012-01-03 2 days |
| 49 | +Addition Operations |
| 50 | +import pandas as pd |
| 51 | + |
| 52 | +s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D')) |
| 53 | +td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ]) |
| 54 | +df = pd.DataFrame(dict(A = s, B = td)) |
| 55 | +df['C']=df['A']+df['B'] |
| 56 | + |
| 57 | +print df |
| 58 | +Its output is as follows − |
| 59 | + A B C |
| 60 | +0 2012-01-01 0 days 2012-01-01 |
| 61 | +1 2012-01-02 1 days 2012-01-03 |
| 62 | +2 2012-01-03 2 days 2012-01-05 |
| 63 | +Subtraction Operation |
| 64 | +import pandas as pd |
| 65 | + |
| 66 | +s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D')) |
| 67 | +td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ]) |
| 68 | +df = pd.DataFrame(dict(A = s, B = td)) |
| 69 | +df['C']=df['A']+df['B'] |
| 70 | +df['D']=df['C']+df['B'] |
| 71 | +print df |
| 72 | +Its output is as follows − |
| 73 | + A B C D |
| 74 | +0 2012-01-01 0 days 2012-01-01 2012-01-01 |
| 75 | +1 2012-01-02 1 days 2012-01-03 2012-01-04 |
| 76 | +2 2012-01-03 2 days 2012-01-05 2012-01-07 |
| 77 | + |
| 78 | + |
0 commit comments