Пример #1
0
def test_where_datetimelike_categorical(request, tz_naive_fixture, using_array_manager):
    # GH#37682
    tz = tz_naive_fixture

    if using_array_manager and tz is None:
        # TODO(ArrayManager) DataFrame.values not yet correctly returning datetime array
        # for categorical with datetime categories
        td.mark_array_manager_not_yet_implemented(request)

    dr = date_range("2001-01-01", periods=3, tz=tz)._with_freq(None)
    lvals = pd.DatetimeIndex([dr[0], dr[1], pd.NaT])
    rvals = pd.Categorical([dr[0], pd.NaT, dr[2]])

    mask = np.array([True, True, False])

    # DatetimeIndex.where
    res = lvals.where(mask, rvals)
    tm.assert_index_equal(res, dr)

    # DatetimeArray.where
    res = lvals._data._where(mask, rvals)
    tm.assert_datetime_array_equal(res, dr._data)

    # Series.where
    res = Series(lvals).where(mask, rvals)
    tm.assert_series_equal(res, Series(dr))

    # DataFrame.where
    res = pd.DataFrame(lvals).where(mask[:, None], pd.DataFrame(rvals))

    tm.assert_frame_equal(res, pd.DataFrame(dr))
Пример #2
0
    def test_from_pandas_array(self):
        arr = pd.array(np.arange(5, dtype=np.int64)) * 3600 * 10**9

        result = DatetimeArray._from_sequence(arr)._with_freq("infer")

        expected = pd.date_range("1970-01-01", periods=5, freq="H")._data
        tm.assert_datetime_array_equal(result, expected)
Пример #3
0
def test_where_datetimelike_categorical(tz_naive_fixture):
    # GH#37682
    tz = tz_naive_fixture

    dr = pd.date_range("2001-01-01", periods=3, tz=tz)._with_freq(None)
    lvals = pd.DatetimeIndex([dr[0], dr[1], pd.NaT])
    rvals = pd.Categorical([dr[0], pd.NaT, dr[2]])

    mask = np.array([True, True, False])

    # DatetimeIndex.where
    res = lvals.where(mask, rvals)
    tm.assert_index_equal(res, dr)

    # DatetimeArray.where
    res = lvals._data.where(mask, rvals)
    tm.assert_datetime_array_equal(res, dr._data)

    # Series.where
    res = Series(lvals).where(mask, rvals)
    tm.assert_series_equal(res, Series(dr))

    # DataFrame.where
    if tz is None:
        res = pd.DataFrame(lvals).where(mask[:, None], pd.DataFrame(rvals))
    else:
        with pytest.xfail(reason="frame._values loses tz"):
            res = pd.DataFrame(lvals).where(mask[:, None], pd.DataFrame(rvals))

    tm.assert_frame_equal(res, pd.DataFrame(dr))
Пример #4
0
def test_datetime_with_timezone(as_asi):
    # see gh-15426
    ts = Timestamp("2016-01-01 12:00:00", tz="US/Pacific")
    exp = DatetimeIndex([ts, ts])._data

    obj = exp.asi8 if as_asi else exp
    res = maybe_downcast_to_dtype(obj, exp.dtype)

    tm.assert_datetime_array_equal(res, exp)
Пример #5
0
    def test_tz_localize_t2d(self):
        dti = pd.date_range("1994-05-12", periods=12, tz="US/Pacific")
        dta = dti._data.reshape(3, 4)
        result = dta.tz_localize(None)

        expected = dta.ravel().tz_localize(None).reshape(dta.shape)
        tm.assert_datetime_array_equal(result, expected)

        roundtrip = expected.tz_localize("US/Pacific")
        tm.assert_datetime_array_equal(roundtrip, dta)
Пример #6
0
def test_astype_datetime(other):
    arr = period_array(["2000", "2001", None], freq="D")
    # slice off the [ns] so that the regex matches.
    if other == "timedelta64[ns]":
        with pytest.raises(TypeError, match=other[:-4]):
            arr.astype(other)

    else:
        # GH#45038 allow period->dt64 because we allow dt64->period
        result = arr.astype(other)
        expected = pd.DatetimeIndex(["2000", "2001", pd.NaT])._data
        tm.assert_datetime_array_equal(result, expected)
Пример #7
0
    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)
Пример #8
0
    def test_round(self, arr1d):
        # GH#24064
        dti = self.index_cls(arr1d)

        result = dti.round(freq="2T")
        expected = dti - pd.Timedelta(minutes=1)
        expected = expected._with_freq(None)
        tm.assert_index_equal(result, expected)

        dta = dti._data
        result = dta.round(freq="2T")
        expected = expected._data._with_freq(None)
        tm.assert_datetime_array_equal(result, expected)
Пример #9
0
    def test_round(self, tz_naive_fixture):
        # GH#24064
        tz = tz_naive_fixture
        dti = pd.date_range("2016-01-01 01:01:00", periods=3, freq="H", tz=tz)

        result = dti.round(freq="2T")
        expected = dti - pd.Timedelta(minutes=1)
        expected = expected._with_freq(None)
        tm.assert_index_equal(result, expected)

        dta = dti._data
        result = dta.round(freq="2T")
        expected = expected._data._with_freq(None)
        tm.assert_datetime_array_equal(result, expected)
Пример #10
0
    def test_mean_2d(self):
        dti = pd.date_range("2016-01-01", periods=6, tz="US/Pacific")
        dta = dti._data.reshape(3, 2)

        result = dta.mean(axis=0)
        expected = dta[1]
        tm.assert_datetime_array_equal(result, expected)

        result = dta.mean(axis=1)
        expected = dta[:, 0] + pd.Timedelta(hours=12)
        tm.assert_datetime_array_equal(result, expected)

        result = dta.mean(axis=None)
        expected = dti.mean()
        assert result == expected
Пример #11
0
    def test_2d(self, order):
        dti = pd.date_range("2016-01-01", periods=6, tz="US/Pacific")
        arr = np.array(dti, dtype=object).reshape(3, 2)
        if order == "F":
            arr = arr.T

        res = sequence_to_dt64ns(arr)
        expected = sequence_to_dt64ns(arr.ravel())

        tm.assert_numpy_array_equal(res[0].ravel(), expected[0])
        assert res[1] == expected[1]
        assert res[2] == expected[2]

        res = DatetimeArray._from_sequence(arr)
        expected = DatetimeArray._from_sequence(arr.ravel()).reshape(arr.shape)
        tm.assert_datetime_array_equal(res, expected)
Пример #12
0
    def test_mean_empty(self, arr1d, skipna):
        arr = arr1d[:0]

        assert arr.mean(skipna=skipna) is NaT

        arr2d = arr.reshape(0, 3)
        result = arr2d.mean(axis=0, skipna=skipna)
        expected = DatetimeArray._from_sequence([NaT, NaT, NaT], dtype=arr.dtype)
        tm.assert_datetime_array_equal(result, expected)

        result = arr2d.mean(axis=1, skipna=skipna)
        expected = arr  # i.e. 1D, empty
        tm.assert_datetime_array_equal(result, expected)

        result = arr2d.mean(axis=None, skipna=skipna)
        assert result is NaT
Пример #13
0
    def test_concat_same_type_different_freq(self):
        # we *can* concatenate DTI with different freqs.
        a = DatetimeArray(
            pd.date_range("2000", periods=2, freq="D", tz="US/Central"))
        b = DatetimeArray(
            pd.date_range("2000", periods=2, freq="H", tz="US/Central"))
        result = DatetimeArray._concat_same_type([a, b])
        expected = DatetimeArray(
            pd.to_datetime([
                "2000-01-01 00:00:00",
                "2000-01-02 00:00:00",
                "2000-01-01 00:00:00",
                "2000-01-01 01:00:00",
            ]).tz_localize("US/Central"))

        tm.assert_datetime_array_equal(result, expected)
Пример #14
0
    def test_shift_fill_value(self):
        dti = pd.date_range("2016-01-01", periods=3)

        dta = dti._data
        expected = DatetimeArray(np.roll(dta._data, 1))

        fv = dta[-1]
        for fill_value in [fv, fv.to_pydatetime(), fv.to_datetime64()]:
            result = dta.shift(1, fill_value=fill_value)
            tm.assert_datetime_array_equal(result, expected)

        dta = dta.tz_localize("UTC")
        expected = expected.tz_localize("UTC")
        fv = dta[-1]
        for fill_value in [fv, fv.to_pydatetime()]:
            result = dta.shift(1, fill_value=fill_value)
            tm.assert_datetime_array_equal(result, expected)
Пример #15
0
    def test_to_timestamp_non_contiguous(self):
        # GH#44100
        dti = date_range("2021-10-18", periods=9, freq="B")
        pi = dti.to_period()

        result = pi[::2].to_timestamp()
        expected = dti[::2]
        tm.assert_index_equal(result, expected)

        result = pi._data[::2].to_timestamp()
        expected = dti._data[::2]
        # TODO: can we get the freq to round-trip?
        tm.assert_datetime_array_equal(result, expected, check_freq=False)

        result = pi[::-1].to_timestamp()
        expected = dti[::-1]
        tm.assert_index_equal(result, expected)

        result = pi._data[::-1].to_timestamp()
        expected = dti._data[::-1]
        tm.assert_datetime_array_equal(result, expected, check_freq=False)

        result = pi[::2][::-1].to_timestamp()
        expected = dti[::2][::-1]
        tm.assert_index_equal(result, expected)

        result = pi._data[::2][::-1].to_timestamp()
        expected = dti._data[::2][::-1]
        tm.assert_datetime_array_equal(result, expected, check_freq=False)