-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
REF: Make PeriodArray an ExtensionArray #22862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
eaadcbc
a05928a
3c0d9ee
63fc3fa
7d5d71c
e5caac6
c194407
eb4506b
1b9fd7a
0fa0ed1
3247ea8
c162cdd
611d378
fb2ff82
25a380f
d04293e
eacad39
70cd3b8
9b22889
87d289a
6369c7f
01551f0
0437940
42ab137
298390f
23e5cfc
9d17fd2
959cd72
b66f617
5669675
2c0311c
012be1c
c3a96d0
67faabc
ff7c06c
c2d57bd
fbde770
1c4bbe7
b395c90
d68a5c5
0c7b704
d26d3d2
e4babea
7f6c144
b4aa4ca
6a70131
9aa077c
411738c
8e0fb69
6d98e85
6d9e150
4899479
634def1
1f18452
2f92b22
23f232c
dd3b8cd
1a7c360
87ecb64
0bde329
2d85a82
438e6b5
a9456fd
ac05365
36ed547
4652ca7
a047a1b
a4a30d7
1441ae6
8003808
5f43753
1c13d0f
bae6b3d
f422cf0
0229d74
aa40cf4
29085e1
00ffddf
e81fa9c
0c8925f
96204a1
82930f7
8d24582
1fc7744
6cd428c
21693e0
b65ffad
1f438e3
f3928fb
089f8ab
700650a
452c229
e3e0e57
78751c2
203d561
eb1c67d
e08aa79
c1ee04b
827e563
ca4a7fd
ed185c0
b3407ac
a4011eb
fc1ca3c
1b1841f
b3b315a
3ab4176
8102475
8c329eb
78d4960
4e3d914
5e4aaa7
7f77563
f88d6f7
7aa78ba
2d737f8
833899a
236b49c
8230347
bf33a57
738acfe
032ec02
77e389a
61031d7
a094b3d
ace4856
fc6a1c7
900afcf
0baa3e9
f95106e
e57e24a
ce1c970
a7e1216
2548d6a
02e3863
1997cff
af2d1de
64f5778
4151510
ac9bd41
5462bd7
c1c6428
7ab2736
bd6f966
8068daf
5691506
575d61a
4065bdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
* docstring on PeriodArray * examples for period_array * remove _box_values_as_index * names * object_dtype * use __sub__
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,12 +114,27 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray): | |
|
||
Users should use :func:`period_array` to create new instances. | ||
|
||
Parameters | ||
---------- | ||
values : Union[PeriodArray, Series[period], ndarary[int], PeriodIndex | ||
The data to store. These should be arrays that can be directly | ||
converted to ordinals without inference or copy (PeriodArray, | ||
ndarray[int64]), or a box around such an array (Series[period], | ||
PeriodIndex). | ||
freq : str or DateOffset | ||
The `freq` to use for the array. Mostly applicable when `values` | ||
is an ndarray of integers, when `freq` is required. When `values` | ||
is a PeriodArray (or box around), it's checked that ``values.freq`` | ||
matches `freq`. | ||
copy : bool, default False | ||
Whether to copy the ordinals before storing. | ||
|
||
Notes | ||
----- | ||
There are two components to a PeriodArray | ||
|
||
- ordinals : integer ndarray | ||
- freq : pd.tseries.offsets.Tick | ||
- freq : pd.tseries.offsets.Offset | ||
|
||
The values are physically stored as a 1-D ndarray of integers. These are | ||
called "ordinals" and represent some kind of offset from a base. | ||
|
@@ -149,7 +164,6 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray): | |
# -------------------------------------------------------------------- | ||
# Constructors | ||
def __init__(self, values, freq=None, copy=False): | ||
# type: (Union[PeriodArray, np.ndarray], Union[str, Tick]) -> None | ||
if isinstance(values, ABCSeries): | ||
values = values._values | ||
if not isinstance(values, type(self)): | ||
|
@@ -317,6 +331,11 @@ def __setitem__( | |
value # type: Union[NaTType, Period, Sequence[Period]] | ||
): | ||
# type: (...) -> None | ||
# n.b. the type on `value` is a bit too restrictive. | ||
# we also accept a sequence of stuff coercible to a PeriodArray | ||
# by period_array, which includes things like ndarray[object], | ||
# ndarray[datetime64ns]. I think ndarray[int] / ndarray[str] won't | ||
# work, since the freq can't be inferred. | ||
if is_list_like(value): | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(key) != len(value) and not com.is_bool_indexer(key): | ||
msg = ("shape mismatch: value array of length '{}' does not " | ||
|
@@ -903,6 +922,19 @@ def period_array(data, freq=None, ordinal=None, copy=False): | |
<PeriodArray> | ||
['2017', '2018', 'NaT'] | ||
Length: 3, dtype: period[A-DEC] | ||
|
||
Integers that look like years are handled | ||
|
||
>>> period_array([2000, 2001, 2002], freq='D') | ||
['2000-01-01', '2001-01-01', '2002-01-01'] | ||
Length: 3, dtype: period[D] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can this case be distinguished from ordinals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. magic? No idea though. That's what scares me about using them. Should I just remove that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it all comes down to whatever In [7]: pd.Period(999, freq='D')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-cd7bbf684368> in <module>
----> 1 pd.Period(999, freq='D')
~/sandbox/pandas/pandas/_libs/tslibs/period.pyx in pandas._libs.tslibs.period.Period.__new__()
2448 value = str(value)
2449 value = value.upper()
-> 2450 dt, _, reso = parse_time_string(value, freq)
2451 if dt is NaT:
2452 ordinal = iNaT
~/sandbox/pandas/pandas/_libs/tslibs/parsing.pyx in pandas._libs.tslibs.parsing.parse_time_string()
126 yearfirst = get_option("display.date_yearfirst")
127
--> 128 res = parse_datetime_string_with_reso(arg, freq=freq,
129 dayfirst=dayfirst,
130 yearfirst=yearfirst)
~/sandbox/pandas/pandas/_libs/tslibs/parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string_with_reso()
152
153 if not _does_string_look_like_datetime(date_string):
--> 154 raise ValueError('Given date string not likely a datetime.')
155
156 try:
ValueError: Given date string not likely a datetime.
In [8]: pd.Period(1000, freq='D')
Out[8]: Period('1000-01-01', 'D') |
||
|
||
Datetime-like strings may also be passed | ||
|
||
>>> period_array(['2000-Q1', '2000-Q2', '2000-Q3', '2000-Q4'], freq='Q') | ||
<PeriodArray> | ||
['2000Q1', '2000Q2', '2000Q3', '2000Q4'] | ||
Length: 4, dtype: period[Q-DEC] | ||
""" | ||
|
||
if data is None and ordinal is None: | ||
|
Uh oh!
There was an error while loading. Please reload this page.