def test_from_datetime64_freq_changes(): # https://github.com/pandas-dev/pandas/issues/23438 arr = pd.date_range("2017", periods=3, freq="D") 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)
def test_to_period_2d(self, arr1d): arr2d = arr1d.reshape(1, -1) warn = None if arr1d.tz is None else UserWarning with tm.assert_produces_warning(warn): result = arr2d.to_period("D") expected = arr1d.to_period("D").reshape(1, -1) tm.assert_period_array_equal(result, expected)
def test_view_i8_to_datetimelike(self): dti = date_range("2000", periods=4, tz="US/Central") ser = Series(dti.asi8) result = ser.view(dti.dtype) tm.assert_datetime_array_equal(result._values, dti._data._with_freq(None)) pi = dti.tz_localize(None).to_period("D") ser = Series(pi.asi8) result = ser.view(pi.dtype) tm.assert_period_array_equal(result._values, pi._data)
def test_period_array_readonly_object(): # https://github.com/pandas-dev/pandas/issues/25403 pa = period_array([pd.Period("2019-01-01")]) arr = np.asarray(pa, dtype="object") arr.setflags(write=False) result = period_array(arr) tm.assert_period_array_equal(result, pa) result = pd.Series(arr) tm.assert_series_equal(result, pd.Series(pa)) result = pd.DataFrame({"A": arr}) tm.assert_frame_equal(result, pd.DataFrame({"A": pa}))
def test_concat_periodarray_2d(): pi = pd.period_range("2016-01-01", periods=36, freq="D") arr = pi._data.reshape(6, 6) result = _concat.concat_compat([arr[:2], arr[2:]], axis=0) tm.assert_period_array_equal(result, arr) result = _concat.concat_compat([arr[:, :2], arr[:, 2:]], axis=1) tm.assert_period_array_equal(result, arr) msg = "all the input array dimensions for the concatenation axis must match exactly" with pytest.raises(ValueError, match=msg): _concat.concat_compat([arr[:, :2], arr[:, 2:]], axis=0) with pytest.raises(ValueError, match=msg): _concat.concat_compat([arr[:2], arr[2:]], axis=1)
def test_setitem(key, value, expected): arr = PeriodArray(np.arange(3), freq="D") expected = PeriodArray(expected, freq="D") arr[key] = value tm.assert_period_array_equal(arr, expected)
def test_astype_period(): arr = period_array(["2000", "2001", None], freq="D") result = arr.astype(PeriodDtype("M")) expected = period_array(["2000", "2001", None], freq="M") tm.assert_period_array_equal(result, expected)