Timedelta#
- class Timedelta#
Represents a duration, the difference between two dates or times.
Example#
import pandasCore as pd
# Create Timedelta
obj = pd.Timedelta(...)
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
Return a tuple of components. |
|
|
Return the days component |
|
|
Return the internal value in nanoseconds (alias for value) |
|
|
Return the hours component (0-23) |
|
|
Return the microseconds component (0-999) |
|
|
Return the milliseconds component (0-999) |
|
|
Return the minutes component (0-59) |
|
|
Return the nanoseconds component (0-999) |
|
|
Return the seconds component (0-59) |
|
|
Total days in the duration |
|
|
Total hours in the duration |
|
|
Total minutes in the duration |
|
|
Total nanoseconds in the duration |
|
|
Return the internal value in nanoseconds |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Create a Timedelta. |
Arithmetic#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Comparison#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
I/O#
Method |
Description |
Example |
|---|---|---|
|
Return the value as nanoseconds (for numpy compatibility) |
|
|
Convert to Python datetime.timedelta object. |
|
|
Convert to numpy timedelta64. |
Conversion#
Method |
Description |
Example |
|---|---|---|
|
View the Timedelta as a different dtype. |
Datetime Methods#
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
Convert Timedelta to target unit resolution. |
|
|
Return a string representation of components |
|
|
Return True if the Timedelta is NaT (Not-a-Timedelta) |
|
|
Return ISO 8601 duration format string. |
|
|
Total seconds in the duration. |
Code Examples#
The following examples are extracted from the test suite.
components (test_timedelta.py:534)
524 f_print_error(f"Exception: {e}")
525 return False
526
527
528def test_components():
529 """Test Timedelta components property"""
530 f_print_header("Test: Timedelta components")
531
532 try:
533 td = pandasCore.Timedelta("1 days 02:30:45")
534 components = td.components
535
536 if components is not None and len(components) == 7:
537 f_print_success(f"components: {components}")
538 # (days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)
539 if components[0] == 1 and components[1] == 2 and components[2] == 30 and components[3] == 45:
540 f_print_success("Components values are correct")
541 else:
542 f_print_error(f"Components values wrong: {components}")
543 return False
544 else:
days (test_timedelta.py:88)
78def test_construction_from_string():
79 """Test Timedelta construction from string"""
80 f_print_header("Test: Timedelta Construction from String")
81
82 try:
83 # Create Timedelta from string
84 td = pandasCore.Timedelta("1 days 02:30:00")
85 f_print_success("Created Timedelta from string '1 days 02:30:00'")
86
87 # Check days component
88 if td.days == 1:
89 f_print_success(f"days is correct: {td.days}")
90 else:
91 f_print_error(f"days is wrong: {td.days}, expected 1")
92 return False
93
94 # Check hours component
95 if td.hours == 2:
96 f_print_success(f"hours is correct: {td.hours}")
97 else:
98 f_print_error(f"hours is wrong: {td.hours}, expected 2")
hours (test_timedelta.py:95)
85 f_print_success("Created Timedelta from string '1 days 02:30:00'")
86
87 # Check days component
88 if td.days == 1:
89 f_print_success(f"days is correct: {td.days}")
90 else:
91 f_print_error(f"days is wrong: {td.days}, expected 1")
92 return False
93
94 # Check hours component
95 if td.hours == 2:
96 f_print_success(f"hours is correct: {td.hours}")
97 else:
98 f_print_error(f"hours is wrong: {td.hours}, expected 2")
99 return False
100
101 # Check minutes component
102 if td.minutes == 30:
103 f_print_success(f"minutes is correct: {td.minutes}")
104 else:
105 f_print_error(f"minutes is wrong: {td.minutes}, expected 30")
minutes (test_timedelta.py:102)
92 return False
93
94 # Check hours component
95 if td.hours == 2:
96 f_print_success(f"hours is correct: {td.hours}")
97 else:
98 f_print_error(f"hours is wrong: {td.hours}, expected 2")
99 return False
100
101 # Check minutes component
102 if td.minutes == 30:
103 f_print_success(f"minutes is correct: {td.minutes}")
104 else:
105 f_print_error(f"minutes is wrong: {td.minutes}, expected 30")
106 return False
107
108 # Check repr
109 repr_str = repr(td)
110 f_print_info(f"repr: {repr_str}")
111
112 # Test short format
seconds (test_timedelta.py:201)
191 return False
192
193 # Check minutes
194 if td.minutes == 30:
195 f_print_success(f"minutes: {td.minutes}")
196 else:
197 f_print_error(f"minutes wrong: {td.minutes}, expected 30")
198 return False
199
200 # Check seconds
201 if td.seconds == 45:
202 f_print_success(f"seconds: {td.seconds}")
203 else:
204 f_print_error(f"seconds wrong: {td.seconds}, expected 45")
205 return False
206
207 return True
208
209 except Exception as e:
210 f_print_error(f"Exception: {e}")
211 return False
value (test_timedelta.py:37)
27def test_construction_default():
28 """Test default Timedelta construction (zero duration)"""
29 f_print_header("Test: Timedelta Default Construction")
30
31 try:
32 # Create default Timedelta (zero duration)
33 td = pandasCore.Timedelta()
34 f_print_success("Created default Timedelta")
35
36 # Check value is 0
37 if td.value == 0:
38 f_print_success(f"value is correct: {td.value}")
39 else:
40 f_print_error(f"value is wrong: {td.value}, expected 0")
41 return False
42
43 # Check repr
44 repr_str = repr(td)
45 f_print_info(f"repr: {repr_str}")
46
47 return True
__init__ (test_timedelta.py:33)
23import pandasCore
24import pandas as pd
25
26
27def test_construction_default():
28 """Test default Timedelta construction (zero duration)"""
29 f_print_header("Test: Timedelta Default Construction")
30
31 try:
32 # Create default Timedelta (zero duration)
33 td = pandasCore.Timedelta()
34 f_print_success("Created default Timedelta")
35
36 # Check value is 0
37 if td.value == 0:
38 f_print_success(f"value is correct: {td.value}")
39 else:
40 f_print_error(f"value is wrong: {td.value}, expected 0")
41 return False
42
43 # Check repr
to_pytimedelta (test_timedelta.py:495)
485 f_print_error(f"Exception: {e}")
486 return False
487
488
489def test_to_pytimedelta():
490 """Test Timedelta to_pytimedelta conversion"""
491 f_print_header("Test: Timedelta to_pytimedelta")
492
493 try:
494 td = pandasCore.Timedelta("1 days 02:30:45")
495 py_td = td.to_pytimedelta()
496
497 # Check if it's a Python timedelta
498 import datetime
499 if isinstance(py_td, datetime.timedelta):
500 f_print_success(f"to_pytimedelta returned: {py_td}")
501 else:
502 f_print_error(f"to_pytimedelta should return datetime.timedelta, got: {type(py_td)}")
503 return False
504
505 # Check days
ceil (test_timedelta.py:285)
275 # Floor to hour
276 td_floor = td.floor("h")
277 if td_floor.hours == 12 and td_floor.minutes == 0:
278 f_print_success(f"floor('h'): {td_floor}")
279 else:
280 f_print_error(f"floor('h') wrong: hours={td_floor.hours}, minutes={td_floor.minutes}")
281 return False
282
283 # Ceil to hour
284 td_ceil = td.ceil("h")
285 if td_ceil.hours == 13 and td_ceil.minutes == 0:
286 f_print_success(f"ceil('h'): {td_ceil}")
287 else:
288 f_print_error(f"ceil('h') wrong: hours={td_ceil.hours}, minutes={td_ceil.minutes}")
289 return False
290
291 # Round to hour (30:30 should round up to 13)
292 td_round = td.round("h")
293 if td_round.hours == 13:
294 f_print_success(f"round('h'): {td_round}")
floor (test_timedelta.py:277)
267def test_rounding_methods():
268 """Test Timedelta rounding methods"""
269 f_print_header("Test: Timedelta Rounding Methods")
270
271 try:
272 # Create Timedelta: 1 day, 12 hours, 30 minutes, 30 seconds
273 td = pandasCore.Timedelta("1 days 12:30:30")
274
275 # Floor to hour
276 td_floor = td.floor("h")
277 if td_floor.hours == 12 and td_floor.minutes == 0:
278 f_print_success(f"floor('h'): {td_floor}")
279 else:
280 f_print_error(f"floor('h') wrong: hours={td_floor.hours}, minutes={td_floor.minutes}")
281 return False
282
283 # Ceil to hour
284 td_ceil = td.ceil("h")
285 if td_ceil.hours == 13 and td_ceil.minutes == 0:
286 f_print_success(f"ceil('h'): {td_ceil}")
round (test_timedelta.py:293)
283 # Ceil to hour
284 td_ceil = td.ceil("h")
285 if td_ceil.hours == 13 and td_ceil.minutes == 0:
286 f_print_success(f"ceil('h'): {td_ceil}")
287 else:
288 f_print_error(f"ceil('h') wrong: hours={td_ceil.hours}, minutes={td_ceil.minutes}")
289 return False
290
291 # Round to hour (30:30 should round up to 13)
292 td_round = td.round("h")
293 if td_round.hours == 13:
294 f_print_success(f"round('h'): {td_round}")
295 else:
296 f_print_error(f"round('h') wrong: hours={td_round.hours}")
297 return False
298
299 return True
300
301 except Exception as e:
302 f_print_error(f"Exception: {e}")
isoformat (test_timedelta.py:252)
242 f_print_error(f"Exception: {e}")
243 return False
244
245
246def test_isoformat():
247 """Test Timedelta isoformat method"""
248 f_print_header("Test: Timedelta isoformat")
249
250 try:
251 td = pandasCore.Timedelta("1 days 02:30:00")
252 iso_str = td.isoformat()
253
254 # Should contain P (period) and D (days)
255 if "P" in iso_str and "D" in iso_str:
256 f_print_success(f"isoformat: {iso_str}")
257 else:
258 f_print_error(f"isoformat wrong: {iso_str}")
259 return False
260
261 return True
total_seconds (test_timedelta.py:64)
54def test_construction_from_nanoseconds():
55 """Test Timedelta construction from nanoseconds"""
56 f_print_header("Test: Timedelta Construction from Nanoseconds")
57
58 try:
59 # Create Timedelta from nanoseconds (1 second = 1e9 nanoseconds)
60 td = pandasCore.Timedelta(1000000000)
61 f_print_success("Created Timedelta from nanoseconds")
62
63 # Check total_seconds
64 total_sec = td.total_seconds()
65 if abs(total_sec - 1.0) < 0.0001:
66 f_print_success(f"total_seconds is correct: {total_sec}")
67 else:
68 f_print_error(f"total_seconds is wrong: {total_sec}, expected 1.0")
69 return False
70
71 return True
72
73 except Exception as e:
74 f_print_error(f"Exception: {e}")