Timestamp#
- class Timestamp#
Pandas-compatible Timestamp representing a single moment in time.
Example#
import pandasCore as pd
# Create Timestamp
obj = pd.Timestamp(...)
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
Return the day of the month (1-31) |
|
|
Return the day of the week (0=Monday, 6=Sunday) |
|
|
Return the day of the year (1-366) |
|
|
Return the day of the week (0=Monday, 6=Sunday) |
|
|
Return the day of the year (1-366) |
|
|
Return the number of days in the month |
|
|
Return the fold value for DST disambiguation |
|
|
Return the hour (0-23) |
|
|
Return True if the year is a leap year |
|
|
Return True if the date is the last day of the month |
|
|
Return True if the date is the first day of the month |
|
|
Return True if the date is the last day of the quarter |
|
|
Return True if the date is the first day of the quarter |
|
|
Return True if the date is the last day of the year |
|
|
Return True if the date is the first day of the year |
|
|
Return True if the Timestamp is NaT (Not a Timestamp) |
|
|
Return the microsecond (0-999999) |
|
|
Return the minute (0-59) |
|
|
Return the month (1-12) |
|
|
Return the nanosecond (0-999) |
|
|
Return the quarter (1-4) |
|
|
Return the second (0-59) |
|
|
Return the timezone name |
|
|
Return the timezone name |
|
|
Return the value as nanoseconds since epoch |
|
|
Return the ISO week number (1-53) |
|
|
Return the ISO week number (1-53) |
|
|
Return the year |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Create a Timestamp from various inputs (pandas-compatible). |
Data Manipulation#
Method |
Description |
Example |
|---|---|---|
|
Return Timestamp with fields replaced. |
Arithmetic#
Method |
Description |
Example |
|---|---|---|
|
||
|
Comparison#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
Time Series#
Method |
Description |
Example |
|---|---|---|
|
Convert Timestamp to a Period. |
|
|
Convert tz-aware Timestamp to another timezone. |
|
|
Localize tz-naive Timestamp to a timezone. |
I/O#
Method |
Description |
Example |
|---|---|---|
|
Return as numpy datetime64 value |
|
|
Return the Julian Date |
|
|
Return as numpy datetime64 value |
|
|
Convert Timestamp to a Python datetime.datetime object. |
Datetime Methods#
Method |
Description |
Example |
|---|---|---|
|
Round up the Timestamp to the specified resolution. |
|
|
Round down the Timestamp to the specified resolution. |
|
|
Normalize Timestamp to midnight, preserving tz information. |
|
|
Round the Timestamp to the specified resolution. |
|
|
Return a string representing the Timestamp, controlled by a format … |
|
|
Return the day of the week (0=Monday, 6=Sunday) |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
Convert Timestamp to target unit resolution. |
|
|
Convert tz-aware Timestamp to another timezone |
|
|
Return ctime() style string |
|
|
Return a datetime.date object |
|
|
Return the name of the day of the week |
|
|
Return the daylight saving time (DST) offset. |
|
|
Return a tuple containing ISO year, week number, and weekday. |
|
|
Return the time formatted according to ISO 8601. |
|
|
Return the ISO weekday (1=Monday, 7=Sunday) |
|
|
Return the name of the month |
|
|
Return a datetime.time object |
|
|
Return POSIX timestamp as float (seconds since epoch). |
|
|
Return time tuple, compatible with time.localtime(). |
|
|
Return a datetime.time object with timezone info |
|
|
Return the proleptic Gregorian ordinal |
|
|
Return the timezone abbreviation |
|
|
Return the UTC offset. |
|
|
Return UTC time tuple |
Code Examples#
The following examples are extracted from the test suite.
day (test_timestamp.py:51)
41 return False
42
43 # Check month
44 if ts.month == 1:
45 f_print_success(f"month is correct: {ts.month}")
46 else:
47 f_print_error(f"month is wrong: {ts.month}, expected 1")
48 return False
49
50 # Check day
51 if ts.day == 15:
52 f_print_success(f"day is correct: {ts.day}")
53 else:
54 f_print_error(f"day is wrong: {ts.day}, expected 15")
55 return False
56
57 # Check hour
58 if ts.hour == 12:
59 f_print_success(f"hour is correct: {ts.hour}")
60 else:
61 f_print_error(f"hour is wrong: {ts.hour}, expected 12")
dayofweek (test_timestamp.py:192)
182def test_derived_properties():
183 """Test Timestamp derived properties"""
184 f_print_header("Test: Timestamp Derived Properties")
185
186 try:
187 # Create a Timestamp for 2021-01-15 (Friday)
188 ts = pandasCore.Timestamp(2021, 1, 15, 12, 0, 0)
189
190 # dayofweek (0=Monday, 6=Sunday) - Friday is 4
191 if ts.dayofweek == 4:
192 f_print_success(f"dayofweek is correct: {ts.dayofweek} (Friday)")
193 else:
194 f_print_error(f"dayofweek is wrong: {ts.dayofweek}, expected 4 (Friday)")
195 return False
196
197 # dayofyear - Jan 15 is day 15
198 if ts.dayofyear == 15:
199 f_print_success(f"dayofyear is correct: {ts.dayofyear}")
200 else:
201 f_print_error(f"dayofyear is wrong: {ts.dayofyear}, expected 15")
dayofyear (test_timestamp.py:199)
189 ts = pandasCore.Timestamp(2021, 1, 15, 12, 0, 0)
190
191 # dayofweek (0=Monday, 6=Sunday) - Friday is 4
192 if ts.dayofweek == 4:
193 f_print_success(f"dayofweek is correct: {ts.dayofweek} (Friday)")
194 else:
195 f_print_error(f"dayofweek is wrong: {ts.dayofweek}, expected 4 (Friday)")
196 return False
197
198 # dayofyear - Jan 15 is day 15
199 if ts.dayofyear == 15:
200 f_print_success(f"dayofyear is correct: {ts.dayofyear}")
201 else:
202 f_print_error(f"dayofyear is wrong: {ts.dayofyear}, expected 15")
203 return False
204
205 # quarter
206 if ts.quarter == 1:
207 f_print_success(f"quarter is correct: {ts.quarter}")
208 else:
209 f_print_error(f"quarter is wrong: {ts.quarter}, expected 1")
hour (test_timestamp.py:58)
48 return False
49
50 # Check day
51 if ts.day == 15:
52 f_print_success(f"day is correct: {ts.day}")
53 else:
54 f_print_error(f"day is wrong: {ts.day}, expected 15")
55 return False
56
57 # Check hour
58 if ts.hour == 12:
59 f_print_success(f"hour is correct: {ts.hour}")
60 else:
61 f_print_error(f"hour is wrong: {ts.hour}, expected 12")
62 return False
63
64 # Check minute
65 if ts.minute == 30:
66 f_print_success(f"minute is correct: {ts.minute}")
67 else:
68 f_print_error(f"minute is wrong: {ts.minute}, expected 30")
microsecond (test_timestamp.py:79)
69 return False
70
71 # Check second
72 if ts.second == 45:
73 f_print_success(f"second is correct: {ts.second}")
74 else:
75 f_print_error(f"second is wrong: {ts.second}, expected 45")
76 return False
77
78 # Check microsecond
79 if ts.microsecond == 123456:
80 f_print_success(f"microsecond is correct: {ts.microsecond}")
81 else:
82 f_print_error(f"microsecond is wrong: {ts.microsecond}, expected 123456")
83 return False
84
85 # Check nanosecond
86 if ts.nanosecond == 789:
87 f_print_success(f"nanosecond is correct: {ts.nanosecond}")
88 else:
89 f_print_error(f"nanosecond is wrong: {ts.nanosecond}, expected 789")
minute (test_timestamp.py:65)
55 return False
56
57 # Check hour
58 if ts.hour == 12:
59 f_print_success(f"hour is correct: {ts.hour}")
60 else:
61 f_print_error(f"hour is wrong: {ts.hour}, expected 12")
62 return False
63
64 # Check minute
65 if ts.minute == 30:
66 f_print_success(f"minute is correct: {ts.minute}")
67 else:
68 f_print_error(f"minute is wrong: {ts.minute}, expected 30")
69 return False
70
71 # Check second
72 if ts.second == 45:
73 f_print_success(f"second is correct: {ts.second}")
74 else:
75 f_print_error(f"second is wrong: {ts.second}, expected 45")
month (test_timestamp.py:44)
34 f_print_success("Created Timestamp from components")
35
36 # Check year
37 if ts.year == 2021:
38 f_print_success(f"year is correct: {ts.year}")
39 else:
40 f_print_error(f"year is wrong: {ts.year}, expected 2021")
41 return False
42
43 # Check month
44 if ts.month == 1:
45 f_print_success(f"month is correct: {ts.month}")
46 else:
47 f_print_error(f"month is wrong: {ts.month}, expected 1")
48 return False
49
50 # Check day
51 if ts.day == 15:
52 f_print_success(f"day is correct: {ts.day}")
53 else:
54 f_print_error(f"day is wrong: {ts.day}, expected 15")
nanosecond (test_timestamp.py:86)
76 return False
77
78 # Check microsecond
79 if ts.microsecond == 123456:
80 f_print_success(f"microsecond is correct: {ts.microsecond}")
81 else:
82 f_print_error(f"microsecond is wrong: {ts.microsecond}, expected 123456")
83 return False
84
85 # Check nanosecond
86 if ts.nanosecond == 789:
87 f_print_success(f"nanosecond is correct: {ts.nanosecond}")
88 else:
89 f_print_error(f"nanosecond is wrong: {ts.nanosecond}, expected 789")
90 return False
91
92 # Check repr
93 repr_str = repr(ts)
94 f_print_info(f"repr: {repr_str}")
95
96 return True
quarter (test_timestamp.py:206)
196 return False
197
198 # dayofyear - Jan 15 is day 15
199 if ts.dayofyear == 15:
200 f_print_success(f"dayofyear is correct: {ts.dayofyear}")
201 else:
202 f_print_error(f"dayofyear is wrong: {ts.dayofyear}, expected 15")
203 return False
204
205 # quarter
206 if ts.quarter == 1:
207 f_print_success(f"quarter is correct: {ts.quarter}")
208 else:
209 f_print_error(f"quarter is wrong: {ts.quarter}, expected 1")
210 return False
211
212 # Create a Timestamp for leap year check (2020)
213 ts_leap = pandasCore.Timestamp(2020, 2, 29, 0, 0, 0)
214 if ts_leap.is_leap_year:
215 f_print_success("is_leap_year is correct: True for 2020")
216 else:
second (test_timestamp.py:72)
62 return False
63
64 # Check minute
65 if ts.minute == 30:
66 f_print_success(f"minute is correct: {ts.minute}")
67 else:
68 f_print_error(f"minute is wrong: {ts.minute}, expected 30")
69 return False
70
71 # Check second
72 if ts.second == 45:
73 f_print_success(f"second is correct: {ts.second}")
74 else:
75 f_print_error(f"second is wrong: {ts.second}, expected 45")
76 return False
77
78 # Check microsecond
79 if ts.microsecond == 123456:
80 f_print_success(f"microsecond is correct: {ts.microsecond}")
81 else:
82 f_print_error(f"microsecond is wrong: {ts.microsecond}, expected 123456")
value (test_date_range_comp.py:42)
32# Helpers
33# =============================================================================
34
35def _compare_lengths(pd_idx, pc_idx, label):
36 """Compare index lengths between pandas and pandasCore."""
37 ctx.assert_equal(len(pd_idx), len(pc_idx), f"{label} length")
38
39
40def _pd_to_ns(ts):
41 """Convert pandas Timestamp to nanoseconds since epoch."""
42 return int(ts.value)
43
44
45def _compare_first_last(pd_idx, pc_idx, label):
46 """Compare first/last values by nanosecond epoch."""
47 pd_first = _pd_to_ns(pd_idx[0])
48 pd_last = _pd_to_ns(pd_idx[len(pd_idx) - 1])
49 pc_first = int(pc_idx[0])
50 pc_last = int(pc_idx[len(pc_idx) - 1])
51 ctx.assert_equal(pd_first, pc_first, f"{label} first value")
52 ctx.assert_equal(pd_last, pc_last, f"{label} last value")
year (test_timestamp.py:37)
27def test_construction_from_components():
28 """Test Timestamp construction from date/time components"""
29 f_print_header("Test: Timestamp Construction from Components")
30
31 try:
32 # Create Timestamp from components
33 ts = pandasCore.Timestamp(2021, 1, 15, 12, 30, 45, 123456, 789)
34 f_print_success("Created Timestamp from components")
35
36 # Check year
37 if ts.year == 2021:
38 f_print_success(f"year is correct: {ts.year}")
39 else:
40 f_print_error(f"year is wrong: {ts.year}, expected 2021")
41 return False
42
43 # Check month
44 if ts.month == 1:
45 f_print_success(f"month is correct: {ts.month}")
46 else:
47 f_print_error(f"month is wrong: {ts.month}, expected 1")
__init__ (test_dateoffset_comp.py:42)
32# ==================== apply() Tests ====================
33# Note: pandas 2.x removed public .apply() on offsets.
34# We compare pandasCore apply() results against ts + offset in pandas.
35
36def test_day_apply():
37 """Test Day.apply() matches pandas ts + offset"""
38 f_print_header("Test: Day.apply()")
39
40 pc_day = pandasCore.Day(n=1)
41 pc_ts = pandasCore.Timestamp('2023-01-15')
42 pc_result = pc_day.apply(pc_ts)
43
44 pd_ts = pd.Timestamp('2023-01-15')
45 pd_result = pd_ts + pd.offsets.Day(n=1)
46
47 ctx.assert_equal(str(pd_result)[:10], str(pc_result)[:10], "Day(1).apply(Jan 15)")
48
49 # Multi-day
50 pc_day5 = pandasCore.Day(n=5)
51 pc_r5 = pc_day5.apply(pc_ts)
replace (test_timestamp.py:412)
402def test_replace_method():
403 """Test Timestamp replace method"""
404 f_print_header("Test: Timestamp replace Method")
405
406 try:
407 ts = pandasCore.Timestamp(2021, 1, 15, 12, 30, 45)
408
409 # Replace year
410 ts_new_year = ts.replace(year=2022)
411 if ts_new_year.year == 2022 and ts_new_year.month == 1 and ts_new_year.day == 15:
412 f_print_success(f"replace(year=2022) correct: {ts_new_year.year}-{ts_new_year.month}-{ts_new_year.day}")
413 else:
414 f_print_error(f"replace(year=2022) wrong")
415 return False
416
417 # Replace month and day
418 ts_new_date = ts.replace(month=6, day=20)
419 if ts_new_date.year == 2021 and ts_new_date.month == 6 and ts_new_date.day == 20:
420 f_print_success(f"replace(month=6, day=20) correct: {ts_new_date.year}-{ts_new_date.month}-{ts_new_date.day}")
ceil (test_timestamp.py:312)
302 # Floor to hour
303 ts_floor = ts.floor("H")
304 if ts_floor.hour == 14 and ts_floor.minute == 0 and ts_floor.second == 0:
305 f_print_success(f"floor('H') correct: {ts_floor.hour}:{ts_floor.minute}:{ts_floor.second}")
306 else:
307 f_print_error(f"floor('H') wrong: {ts_floor.hour}:{ts_floor.minute}:{ts_floor.second}")
308 return False
309
310 # Ceil to hour
311 ts_ceil = ts.ceil("H")
312 if ts_ceil.hour == 15 and ts_ceil.minute == 0:
313 f_print_success(f"ceil('H') correct: {ts_ceil.hour}:{ts_ceil.minute}:{ts_ceil.second}")
314 else:
315 f_print_error(f"ceil('H') wrong: {ts_ceil.hour}:{ts_ceil.minute}:{ts_ceil.second}")
316 return False
317
318 # Round to hour (37 minutes rounds down)
319 ts_round = ts.round("H")
320 if ts_round.hour == 15:
321 f_print_success(f"round('H') correct: {ts_round.hour}:{ts_round.minute}")
floor (test_timestamp.py:304)
294def test_rounding_methods():
295 """Test Timestamp rounding methods"""
296 f_print_header("Test: Timestamp Rounding Methods")
297
298 try:
299 # Create a Timestamp with all components
300 ts = pandasCore.Timestamp(2021, 1, 15, 14, 37, 45, 123456, 789)
301
302 # Floor to hour
303 ts_floor = ts.floor("H")
304 if ts_floor.hour == 14 and ts_floor.minute == 0 and ts_floor.second == 0:
305 f_print_success(f"floor('H') correct: {ts_floor.hour}:{ts_floor.minute}:{ts_floor.second}")
306 else:
307 f_print_error(f"floor('H') wrong: {ts_floor.hour}:{ts_floor.minute}:{ts_floor.second}")
308 return False
309
310 # Ceil to hour
311 ts_ceil = ts.ceil("H")
312 if ts_ceil.hour == 15 and ts_ceil.minute == 0:
313 f_print_success(f"ceil('H') correct: {ts_ceil.hour}:{ts_ceil.minute}:{ts_ceil.second}")
normalize (test_timestamp.py:328)
318 # Round to hour (37 minutes rounds down)
319 ts_round = ts.round("H")
320 if ts_round.hour == 15:
321 f_print_success(f"round('H') correct: {ts_round.hour}:{ts_round.minute}")
322 else:
323 f_print_error(f"round('H') wrong: {ts_round.hour}")
324 return False
325
326 # Normalize (floor to day)
327 ts_norm = ts.normalize()
328 if ts_norm.hour == 0 and ts_norm.minute == 0 and ts_norm.second == 0:
329 f_print_success("normalize() correct: midnight")
330 else:
331 f_print_error(f"normalize() wrong: {ts_norm.hour}:{ts_norm.minute}:{ts_norm.second}")
332 return False
333
334 return True
335
336 except Exception as e:
337 f_print_error(f"Exception: {e}")
round (test_timestamp.py:320)
310 # Ceil to hour
311 ts_ceil = ts.ceil("H")
312 if ts_ceil.hour == 15 and ts_ceil.minute == 0:
313 f_print_success(f"ceil('H') correct: {ts_ceil.hour}:{ts_ceil.minute}:{ts_ceil.second}")
314 else:
315 f_print_error(f"ceil('H') wrong: {ts_ceil.hour}:{ts_ceil.minute}:{ts_ceil.second}")
316 return False
317
318 # Round to hour (37 minutes rounds down)
319 ts_round = ts.round("H")
320 if ts_round.hour == 15:
321 f_print_success(f"round('H') correct: {ts_round.hour}:{ts_round.minute}")
322 else:
323 f_print_error(f"round('H') wrong: {ts_round.hour}")
324 return False
325
326 # Normalize (floor to day)
327 ts_norm = ts.normalize()
328 if ts_norm.hour == 0 and ts_norm.minute == 0 and ts_norm.second == 0:
329 f_print_success("normalize() correct: midnight")
strftime (test_timestamp.py:358)
348 # isoformat
349 iso_str = ts.isoformat()
350 if "2021-01-15" in iso_str and "12:30:45" in iso_str:
351 f_print_success(f"isoformat() correct: {iso_str}")
352 else:
353 f_print_error(f"isoformat() wrong: {iso_str}")
354 return False
355
356 # strftime
357 str_time = ts.strftime("%Y/%m/%d")
358 if str_time == "2021/01/15":
359 f_print_success(f"strftime() correct: {str_time}")
360 else:
361 f_print_error(f"strftime() wrong: {str_time}, expected '2021/01/15'")
362 return False
363
364 # isocalendar
365 iso_cal = ts.isocalendar()
366 if len(iso_cal) == 3:
367 f_print_success(f"isocalendar() correct: {iso_cal}")
day_name (test_timestamp.py:382)
372 # timestamp (POSIX)
373 posix_ts = ts.timestamp()
374 if posix_ts > 0:
375 f_print_success(f"timestamp() correct: {posix_ts}")
376 else:
377 f_print_error(f"timestamp() wrong: {posix_ts}")
378 return False
379
380 # day_name
381 day_name = ts.day_name()
382 if day_name == "Friday":
383 f_print_success(f"day_name() correct: {day_name}")
384 else:
385 f_print_error(f"day_name() wrong: {day_name}, expected 'Friday'")
386 return False
387
388 # month_name
389 month_name = ts.month_name()
390 if month_name == "January":
391 f_print_success(f"month_name() correct: {month_name}")
isocalendar (test_timestamp.py:366)
356 # strftime
357 str_time = ts.strftime("%Y/%m/%d")
358 if str_time == "2021/01/15":
359 f_print_success(f"strftime() correct: {str_time}")
360 else:
361 f_print_error(f"strftime() wrong: {str_time}, expected '2021/01/15'")
362 return False
363
364 # isocalendar
365 iso_cal = ts.isocalendar()
366 if len(iso_cal) == 3:
367 f_print_success(f"isocalendar() correct: {iso_cal}")
368 else:
369 f_print_error(f"isocalendar() wrong format")
370 return False
371
372 # timestamp (POSIX)
373 posix_ts = ts.timestamp()
374 if posix_ts > 0:
375 f_print_success(f"timestamp() correct: {posix_ts}")
isoformat (test_timestamp.py:350)
340def test_conversion_methods():
341 """Test Timestamp conversion methods"""
342 f_print_header("Test: Timestamp Conversion Methods")
343
344 try:
345 ts = pandasCore.Timestamp(2021, 1, 15, 12, 30, 45)
346
347 # isoformat
348 iso_str = ts.isoformat()
349 if "2021-01-15" in iso_str and "12:30:45" in iso_str:
350 f_print_success(f"isoformat() correct: {iso_str}")
351 else:
352 f_print_error(f"isoformat() wrong: {iso_str}")
353 return False
354
355 # strftime
356 str_time = ts.strftime("%Y/%m/%d")
357 if str_time == "2021/01/15":
358 f_print_success(f"strftime() correct: {str_time}")
month_name (test_timestamp.py:390)
380 # day_name
381 day_name = ts.day_name()
382 if day_name == "Friday":
383 f_print_success(f"day_name() correct: {day_name}")
384 else:
385 f_print_error(f"day_name() wrong: {day_name}, expected 'Friday'")
386 return False
387
388 # month_name
389 month_name = ts.month_name()
390 if month_name == "January":
391 f_print_success(f"month_name() correct: {month_name}")
392 else:
393 f_print_error(f"month_name() wrong: {month_name}, expected 'January'")
394 return False
395
396 return True
397
398 except Exception as e:
399 f_print_error(f"Exception: {e}")
timestamp (test_timestamp.py:374)
364 # isocalendar
365 iso_cal = ts.isocalendar()
366 if len(iso_cal) == 3:
367 f_print_success(f"isocalendar() correct: {iso_cal}")
368 else:
369 f_print_error(f"isocalendar() wrong format")
370 return False
371
372 # timestamp (POSIX)
373 posix_ts = ts.timestamp()
374 if posix_ts > 0:
375 f_print_success(f"timestamp() correct: {posix_ts}")
376 else:
377 f_print_error(f"timestamp() wrong: {posix_ts}")
378 return False
379
380 # day_name
381 day_name = ts.day_name()
382 if day_name == "Friday":
383 f_print_success(f"day_name() correct: {day_name}")