Period#
- class Period#
Represents a period of time.
Example#
import pandasCore as pd
# Create Period
obj = pd.Period(...)
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
Return the day of month (1-31) |
|
|
Return the day of week (0=Monday, 6=Sunday) |
|
|
Return the day of year (1-366) |
|
|
Return the day of week (0=Monday, 6=Sunday) |
|
|
Return the day of year (1-366) |
|
|
Return the number of days in the current month |
|
|
Return the end time of this period as a Timestamp |
|
|
Return the frequency string |
|
|
Return the frequency string |
|
|
Return the hour (0-23) |
|
|
Return True if the year is a leap year |
|
|
Return the minute (0-59) |
|
|
Return the month (1-12) |
|
|
Return the ordinal value of this period |
|
|
Return the quarter (1-4) |
|
|
Return the fiscal year |
|
|
Return the second (0-59) |
|
|
Return the start time of this period as a Timestamp |
|
|
Return the week of year (1-53) |
|
|
Return the week of year (1-53) |
|
|
Return the year this period belongs to |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Represents a period of time. |
Arithmetic#
Method |
Description |
Example |
|---|---|---|
|
||
|
Comparison#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
Time Series#
Datetime Methods#
Method |
Description |
Example |
|---|---|---|
|
Convert to a string representation using the specified format. |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
Return True if the Period is NaT (Not a Time) |
Code Examples#
The following examples are extracted from the test suite.
end_time (test_period.py:240)
230 else:
231 f_print_error(f"Start timestamp year/month wrong")
232 return False
233
234 # Convert to timestamp (end)
235 ts_end = p.to_timestamp(how='end')
236 f_print_success(f"to_timestamp(how='end'): {ts_end}")
237
238 # Check start_time and end_time properties
239 f_print_info(f"start_time: {p.start_time}")
240 f_print_info(f"end_time: {p.end_time}")
241
242 return True
243
244 except Exception as e:
245 f_print_error(f"Exception: {e}")
246 return False
247
248
249def test_period_arithmetic():
250 """Test Period arithmetic operations"""
freqstr (test_period.py:89)
79 return False
80
81 # Check quarter
82 if p.quarter == 2:
83 f_print_success(f"quarter is correct: {p.quarter}")
84 else:
85 f_print_error(f"quarter is wrong: {p.quarter}, expected 2")
86 return False
87
88 # Check freqstr
89 if p.freqstr == 'M':
90 f_print_success(f"freqstr is correct: {p.freqstr}")
91 else:
92 f_print_error(f"freqstr is wrong: {p.freqstr}, expected 'M'")
93 return False
94
95 # Test daily period with more properties
96 p2 = pandasCore.Period('2020-06-15', 'D')
97 f_print_info(f"Daily period: {p2}")
98 f_print_info(f" year={p2.year}, month={p2.month}, day={p2.day}")
99 f_print_info(f" dayofweek={p2.dayofweek}, dayofyear={p2.dayofyear}")
month (test_period.py:75)
65 p = pandasCore.Period('2020-06', 'M')
66
67 # Check year
68 if p.year == 2020:
69 f_print_success(f"year is correct: {p.year}")
70 else:
71 f_print_error(f"year is wrong: {p.year}, expected 2020")
72 return False
73
74 # Check month
75 if p.month == 6:
76 f_print_success(f"month is correct: {p.month}")
77 else:
78 f_print_error(f"month is wrong: {p.month}, expected 6")
79 return False
80
81 # Check quarter
82 if p.quarter == 2:
83 f_print_success(f"quarter is correct: {p.quarter}")
84 else:
85 f_print_error(f"quarter is wrong: {p.quarter}, expected 2")
ordinal (test_periodindex.py:118)
108def test_shift():
109 """Test shift method"""
110 f_print_header("Test: shift")
111
112 pi = pandasCore.PeriodIndex([0, 1, 2], freq="D")
113 shifted = pi.shift(1)
114
115 expected = [1, 2, 3]
116 ctx.assert_list_equal(expected, [p.ordinal for p in shifted.tolist()], "shift by 1")
117
118 shifted_neg = pi.shift(-1)
119 expected_neg = [-1, 0, 1]
120 ctx.assert_list_equal(expected_neg, [p.ordinal for p in shifted_neg.tolist()], "shift by -1")
121
122
123def test_strftime():
124 """Test strftime method"""
125 f_print_header("Test: strftime")
quarter (test_period.py:82)
72 return False
73
74 # Check month
75 if p.month == 6:
76 f_print_success(f"month is correct: {p.month}")
77 else:
78 f_print_error(f"month is wrong: {p.month}, expected 6")
79 return False
80
81 # Check quarter
82 if p.quarter == 2:
83 f_print_success(f"quarter is correct: {p.quarter}")
84 else:
85 f_print_error(f"quarter is wrong: {p.quarter}, expected 2")
86 return False
87
88 # Check freqstr
89 if p.freqstr == 'M':
90 f_print_success(f"freqstr is correct: {p.freqstr}")
91 else:
92 f_print_error(f"freqstr is wrong: {p.freqstr}, expected 'M'")
start_time (test_period.py:239)
229 f_print_success(f"Start timestamp year/month correct: {ts_start.year}-{ts_start.month}")
230 else:
231 f_print_error(f"Start timestamp year/month wrong")
232 return False
233
234 # Convert to timestamp (end)
235 ts_end = p.to_timestamp(how='end')
236 f_print_success(f"to_timestamp(how='end'): {ts_end}")
237
238 # Check start_time and end_time properties
239 f_print_info(f"start_time: {p.start_time}")
240 f_print_info(f"end_time: {p.end_time}")
241
242 return True
243
244 except Exception as e:
245 f_print_error(f"Exception: {e}")
246 return False
247
248
249def test_period_arithmetic():
year (test_period.py:68)
58def test_period_properties():
59 """Test Period component properties"""
60 f_print_header("Test: Period Properties")
61
62 try:
63 # Test monthly period
64 p = pandasCore.Period('2020-06', 'M')
65
66 # Check year
67 if p.year == 2020:
68 f_print_success(f"year is correct: {p.year}")
69 else:
70 f_print_error(f"year is wrong: {p.year}, expected 2020")
71 return False
72
73 # Check month
74 if p.month == 6:
75 f_print_success(f"month is correct: {p.month}")
76 else:
77 f_print_error(f"month is wrong: {p.month}, expected 6")
__init__ (test_period.py:33)
23import pandasCore
24import pandas as pd
25
26
27def test_period_construction():
28 """Test Period construction from various inputs"""
29 f_print_header("Test: Period Construction")
30
31 try:
32 # Test from period string and frequency
33 p1 = pandasCore.Period('2020-01', 'M')
34 f_print_success(f"Created Period from string: {p1}")
35
36 # Test from ordinal and frequency
37 p2 = pandasCore.Period(0, 'D')
38 f_print_success(f"Created Period from ordinal: {p2}")
39
40 # Test from year/month components
41 p3 = pandasCore.Period(year=2020, month=6, day=15, freq='D')
42 f_print_success(f"Created Period from components: {p3}")
asfreq (test_period.py:149)
139def test_period_asfreq():
140 """Test Period.asfreq() method"""
141 f_print_header("Test: Period.asfreq()")
142
143 try:
144 # Create monthly period
145 p = pandasCore.Period('2020-01', 'M')
146 f_print_info(f"Original period: {p}")
147
148 # Convert to daily frequency (end of month)
149 p_day_end = p.asfreq('D', 'E')
150 f_print_success(f"asfreq('D', 'E'): {p_day_end}")
151
152 # The end of January 2020 should be day 31
153 if p_day_end.day == 31:
154 f_print_success(f"End of month day is correct: {p_day_end.day}")
155 else:
156 f_print_error(f"End of month day is wrong: {p_day_end.day}, expected 31")
157 return False
158
159 # Convert to daily frequency (start of month)
to_timestamp (test_period.py:224)
214def test_period_to_timestamp():
215 """Test Period.to_timestamp() method"""
216 f_print_header("Test: Period.to_timestamp()")
217
218 try:
219 p = pandasCore.Period('2020-01', 'M')
220 f_print_info(f"Original period: {p}")
221
222 # Convert to timestamp (start)
223 ts_start = p.to_timestamp(how='start')
224 f_print_success(f"to_timestamp(how='start'): {ts_start}")
225
226 # Verify year and month
227 if ts_start.year == 2020 and ts_start.month == 1:
228 f_print_success(f"Start timestamp year/month correct: {ts_start.year}-{ts_start.month}")
229 else:
230 f_print_error(f"Start timestamp year/month wrong")
231 return False
232
233 # Convert to timestamp (end)
strftime (test_period.py:189)
179def test_period_strftime():
180 """Test Period.strftime() method"""
181 f_print_header("Test: Period.strftime()")
182
183 try:
184 p = pandasCore.Period('2020-06-15', 'D')
185
186 # Test various format strings
187 result1 = p.strftime('%Y-%m-%d')
188 f_print_success(f"strftime('%Y-%m-%d'): {result1}")
189 if result1 == '2020-06-15':
190 f_print_success("Format '%Y-%m-%d' is correct")
191 else:
192 f_print_error(f"Format '%Y-%m-%d' is wrong: {result1}")
193 return False
194
195 result2 = p.strftime('%Y/%m')
196 f_print_success(f"strftime('%Y/%m'): {result2}")
197 if result2 == '2020/06':