Skip to content
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

BUG: Allow freq conversion from dt64 to period #23460

Merged
merged 1 commit into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 7 additions & 16 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,24 +967,15 @@ def dt64arr_to_periodarr(data, freq, tz=None):
if data.dtype != np.dtype('M8[ns]'):
raise ValueError('Wrong dtype: %s' % data.dtype)

if freq is not None:
freq = Period._maybe_convert_freq(freq)
if freq is None:
if isinstance(data, ABCIndexClass):
data, freq = data._values, data.freq
elif isinstance(data, ABCSeries):
data, freq = data._values, data.dt.freq

if isinstance(data, ABCIndexClass):
if freq is None:
freq = data.freq
elif freq != data.freq:
msg = DIFFERENT_FREQ_INDEX.format(freq.freqstr, data.freq.freqstr)
raise IncompatibleFrequency(msg)
data = data._values
freq = Period._maybe_convert_freq(freq)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily for this PR, but it isn't obvious when to use this versus just to_offset. Should be clarified and possibly de-duplicated. (and i think de-method-ed)


elif isinstance(data, ABCSeries):
if freq is None:
freq = data.dt.freq
elif freq != data.dt.freq:
msg = DIFFERENT_FREQ_INDEX.format(freq.freqstr,
data.dt.freq.freqstr)
raise IncompatibleFrequency(msg)
if isinstance(data, (ABCIndexClass, ABCSeries)):
data = data._values

base, mult = frequencies.get_freq_code(freq)
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/arrays/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ def test_period_array_ok(data, freq, expected):
tm.assert_numpy_array_equal(result, expected)


def test_from_datetime64_raises():
def test_from_datetime64_freq_changes():
# https://github.com/pandas-dev/pandas/issues/23438
arr = pd.date_range("2017", periods=3, freq="D")
with tm.assert_raises_regex(IncompatibleFrequency, "freq"):
PeriodArray._from_datetime64(arr, freq="M")
result = PeriodArray._from_datetime64(arr, freq="M")
expected = period_array(['2017-01-01', '2017-01-01', '2017-01-01'],
freq="M")
tm.assert_period_array_equal(result, expected)


@pytest.mark.parametrize("data, freq, msg", [
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/period/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ def test_constructor_datetime64arr(self):

pytest.raises(ValueError, PeriodIndex, vals, freq='D')

@pytest.mark.parametrize('box', [None, 'series', 'index'])
def test_constructor_datetime64arr_ok(self, box):
# https://github.com/pandas-dev/pandas/issues/23438
data = pd.date_range('2017', periods=4, freq="M")
if box is None:
data = data._values
elif box == 'series':
data = pd.Series(data)

result = PeriodIndex(data, freq='D')
expected = PeriodIndex([
'2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30'
], freq="D")
tm.assert_index_equal(result, expected)

def test_constructor_dtype(self):
# passing a dtype with a tz should localize
idx = PeriodIndex(['2013-01', '2013-03'], dtype='period[M]')
Expand Down