示例#1
0
def test_conv_read_write(setup_path):
    with tm.ensure_clean() as path:

        def roundtrip(key, obj, **kwargs):
            obj.to_hdf(path, key, **kwargs)
            return read_hdf(path, key)

        o = tm.makeTimeSeries()
        tm.assert_series_equal(o, roundtrip("series", o))

        o = tm.makeStringSeries()
        tm.assert_series_equal(o, roundtrip("string_series", o))

        o = tm.makeDataFrame()
        tm.assert_frame_equal(o, roundtrip("frame", o))

        # table
        df = DataFrame({"A": range(5), "B": range(5)})
        df.to_hdf(path, "table", append=True)
        result = read_hdf(path, "table", where=["index>2"])
        tm.assert_frame_equal(df[df.index > 2], result)
示例#2
0
    def test_squeeze(self):
        # noop
        for s in [
                tm.makeFloatSeries(),
                tm.makeStringSeries(),
                tm.makeObjectSeries()
        ]:
            tm.assert_series_equal(s.squeeze(), s)
        for df in [tm.makeTimeDataFrame()]:
            tm.assert_frame_equal(df.squeeze(), df)

        # squeezing
        df = tm.makeTimeDataFrame().reindex(columns=["A"])
        tm.assert_series_equal(df.squeeze(), df["A"])

        # don't fail with 0 length dimensions GH11229 & GH8999
        empty_series = Series([], name="five", dtype=np.float64)
        empty_frame = DataFrame([empty_series])
        tm.assert_series_equal(empty_series, empty_series.squeeze())
        tm.assert_series_equal(empty_series, empty_frame.squeeze())

        # axis argument
        df = tm.makeTimeDataFrame(nper=1).iloc[:, :1]
        assert df.shape == (1, 1)
        tm.assert_series_equal(df.squeeze(axis=0), df.iloc[0])
        tm.assert_series_equal(df.squeeze(axis="index"), df.iloc[0])
        tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0])
        tm.assert_series_equal(df.squeeze(axis="columns"), df.iloc[:, 0])
        assert df.squeeze() == df.iloc[0, 0]
        msg = "No axis named 2 for object type DataFrame"
        with pytest.raises(ValueError, match=msg):
            df.squeeze(axis=2)
        msg = "No axis named x for object type DataFrame"
        with pytest.raises(ValueError, match=msg):
            df.squeeze(axis="x")

        df = tm.makeTimeDataFrame(3)
        tm.assert_frame_equal(df.squeeze(axis=0), df)
示例#3
0
 def test_invert(self):
     ser = tm.makeStringSeries()
     ser.name = "series"
     tm.assert_series_equal(-(ser < 0), ~(ser < 0))
示例#4
0
 def test_neg(self):
     ser = tm.makeStringSeries()
     ser.name = "series"
     tm.assert_series_equal(-ser, -1 * ser)
示例#5
0
 def test_transpose(self):
     for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries()]:
         # calls implementation in pandas/core/base.py
         tm.assert_series_equal(s.transpose(), s)
     for df in [tm.makeTimeDataFrame()]:
         tm.assert_frame_equal(df.transpose().transpose(), df)
示例#6
0
    def setup_method(self):
        self.ts = tm.makeTimeSeries()  # Was at top level in test_series
        self.ts.name = "ts"

        self.series = tm.makeStringSeries()
        self.series.name = "series"
示例#7
0
 def test_max(self):
     string_series = tm.makeStringSeries().rename("series")
     self._check_stat_op("max", np.max, string_series, check_objects=True)
示例#8
0
 def test_prod(self):
     string_series = tm.makeStringSeries().rename("series")
     self._check_stat_op("prod", np.prod, string_series)
示例#9
0
 def test_mean(self):
     string_series = tm.makeStringSeries().rename("series")
     self._check_stat_op("mean", np.mean, string_series)
示例#10
0
 def test_sum(self):
     string_series = tm.makeStringSeries().rename("series")
     self._check_stat_op("sum", np.sum, string_series, check_allna=False)
示例#11
0
class TestNDFrame:
    # tests that don't fit elsewhere

    @pytest.mark.parametrize(
        "ser",
        [tm.makeFloatSeries(),
         tm.makeStringSeries(),
         tm.makeObjectSeries()])
    def test_squeeze_series_noop(self, ser):
        # noop
        tm.assert_series_equal(ser.squeeze(), ser)

    def test_squeeze_frame_noop(self):
        # noop
        df = tm.makeTimeDataFrame()
        tm.assert_frame_equal(df.squeeze(), df)

    def test_squeeze_frame_reindex(self):
        # squeezing
        df = tm.makeTimeDataFrame().reindex(columns=["A"])
        tm.assert_series_equal(df.squeeze(), df["A"])

    def test_squeeze_0_len_dim(self):
        # don't fail with 0 length dimensions GH11229 & GH8999
        empty_series = Series([], name="five", dtype=np.float64)
        empty_frame = DataFrame([empty_series])
        tm.assert_series_equal(empty_series, empty_series.squeeze())
        tm.assert_series_equal(empty_series, empty_frame.squeeze())

    def test_squeeze_axis(self):
        # axis argument
        df = tm.makeTimeDataFrame(nper=1).iloc[:, :1]
        assert df.shape == (1, 1)
        tm.assert_series_equal(df.squeeze(axis=0), df.iloc[0])
        tm.assert_series_equal(df.squeeze(axis="index"), df.iloc[0])
        tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0])
        tm.assert_series_equal(df.squeeze(axis="columns"), df.iloc[:, 0])
        assert df.squeeze() == df.iloc[0, 0]
        msg = "No axis named 2 for object type DataFrame"
        with pytest.raises(ValueError, match=msg):
            df.squeeze(axis=2)
        msg = "No axis named x for object type DataFrame"
        with pytest.raises(ValueError, match=msg):
            df.squeeze(axis="x")

    def test_squeeze_axis_len_3(self):
        df = tm.makeTimeDataFrame(3)
        tm.assert_frame_equal(df.squeeze(axis=0), df)

    def test_numpy_squeeze(self):
        s = tm.makeFloatSeries()
        tm.assert_series_equal(np.squeeze(s), s)

        df = tm.makeTimeDataFrame().reindex(columns=["A"])
        tm.assert_series_equal(np.squeeze(df), df["A"])

    @pytest.mark.parametrize(
        "ser",
        [tm.makeFloatSeries(),
         tm.makeStringSeries(),
         tm.makeObjectSeries()])
    def test_transpose_series(self, ser):
        # calls implementation in pandas/core/base.py
        tm.assert_series_equal(ser.transpose(), ser)

    def test_transpose_frame(self):
        df = tm.makeTimeDataFrame()
        tm.assert_frame_equal(df.transpose().transpose(), df)

    def test_numpy_transpose(self, frame_or_series):

        obj = tm.makeTimeDataFrame()
        obj = tm.get_obj(obj, frame_or_series)

        if frame_or_series is Series:
            # 1D -> np.transpose is no-op
            tm.assert_series_equal(np.transpose(obj), obj)

        # round-trip preserved
        tm.assert_equal(np.transpose(np.transpose(obj)), obj)

        msg = "the 'axes' parameter is not supported"
        with pytest.raises(ValueError, match=msg):
            np.transpose(obj, axes=1)

    @pytest.mark.parametrize(
        "ser",
        [tm.makeFloatSeries(),
         tm.makeStringSeries(),
         tm.makeObjectSeries()])
    def test_take_series(self, ser):
        indices = [1, 5, -2, 6, 3, -1]
        out = ser.take(indices)
        expected = Series(
            data=ser.values.take(indices),
            index=ser.index.take(indices),
            dtype=ser.dtype,
        )
        tm.assert_series_equal(out, expected)

    def test_take_frame(self):
        indices = [1, 5, -2, 6, 3, -1]
        df = tm.makeTimeDataFrame()
        out = df.take(indices)
        expected = DataFrame(
            data=df.values.take(indices, axis=0),
            index=df.index.take(indices),
            columns=df.columns,
        )
        tm.assert_frame_equal(out, expected)

    def test_take_invalid_kwargs(self, frame_or_series):
        indices = [-3, 2, 0, 1]

        obj = tm.makeTimeDataFrame()
        obj = tm.get_obj(obj, frame_or_series)

        msg = r"take\(\) got an unexpected keyword argument 'foo'"
        with pytest.raises(TypeError, match=msg):
            obj.take(indices, foo=2)

        msg = "the 'out' parameter is not supported"
        with pytest.raises(ValueError, match=msg):
            obj.take(indices, out=indices)

        msg = "the 'mode' parameter is not supported"
        with pytest.raises(ValueError, match=msg):
            obj.take(indices, mode="clip")

    @pytest.mark.parametrize("is_copy", [True, False])
    def test_depr_take_kwarg_is_copy(self, is_copy, frame_or_series):
        # GH 27357
        obj = DataFrame({"A": [1, 2, 3]})
        obj = tm.get_obj(obj, frame_or_series)

        msg = (
            "is_copy is deprecated and will be removed in a future version. "
            "'take' always returns a copy, so there is no need to specify this."
        )
        with tm.assert_produces_warning(FutureWarning) as w:
            obj.take([0, 1], is_copy=is_copy)

        assert w[0].message.args[0] == msg

    def test_axis_classmethods(self, frame_or_series):
        box = frame_or_series
        obj = box(dtype=object)
        values = box._AXIS_TO_AXIS_NUMBER.keys()
        for v in values:
            assert obj._get_axis_number(v) == box._get_axis_number(v)
            assert obj._get_axis_name(v) == box._get_axis_name(v)
            assert obj._get_block_manager_axis(
                v) == box._get_block_manager_axis(v)

    def test_axis_names_deprecated(self, frame_or_series):
        # GH33637
        box = frame_or_series
        obj = box(dtype=object)
        msg = "_AXIS_NAMES has been deprecated"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            obj._AXIS_NAMES

    def test_axis_numbers_deprecated(self, frame_or_series):
        # GH33637
        box = frame_or_series
        obj = box(dtype=object)
        msg = "_AXIS_NUMBERS has been deprecated"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            obj._AXIS_NUMBERS

    def test_flags_identity(self, frame_or_series):
        obj = Series([1, 2])
        if frame_or_series is DataFrame:
            obj = obj.to_frame()

        assert obj.flags is obj.flags
        obj2 = obj.copy()
        assert obj2.flags is not obj.flags

    def test_slice_shift_deprecated(self, frame_or_series):
        # GH 37601
        obj = DataFrame({"A": [1, 2, 3, 4]})
        obj = tm.get_obj(obj, frame_or_series)

        with tm.assert_produces_warning(FutureWarning):
            obj.slice_shift()
示例#12
0
def series():
    return tm.makeStringSeries(name="series")
示例#13
0
    with cf.option_context("mode.use_inf_as_na", True):
        assert not notna_f(np.inf)
        assert not notna_f(-np.inf)

        arr = np.array([1.5, np.inf, 3.5, -np.inf])
        result = notna_f(arr)
        assert result.sum() == 2


@pytest.mark.parametrize("null_func", [notna, notnull, isna, isnull])
@pytest.mark.parametrize(
    "ser",
    [
        tm.makeFloatSeries(),
        tm.makeStringSeries(),
        tm.makeObjectSeries(),
        tm.makeTimeSeries(),
        tm.makePeriodSeries(),
    ],
)
def test_null_check_is_series(null_func, ser):
    with cf.option_context("mode.use_inf_as_na", False):
        assert isinstance(null_func(ser), Series)


class TestIsNA:
    def test_0d_array(self):
        assert isna(np.array(np.nan))
        assert not isna(np.array(0.0))
        assert not isna(np.array(0))