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)
def object_series(): """ Fixture for Series of dtype object with Index of unique strings """ s = tm.makeObjectSeries() s.name = "objects" return s
def test_isna_isnull(self, isna_f): assert not isna_f(1.0) assert isna_f(None) assert isna_f(np.NaN) assert float("nan") assert not isna_f(np.inf) assert not isna_f(-np.inf) # type assert not isna_f(type(pd.Series(dtype=object))) assert not isna_f(type(pd.Series(dtype=np.float64))) assert not isna_f(type(pd.DataFrame())) # series for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries(), ]: assert isinstance(isna_f(s), Series) # frame for df in [ tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame(), ]: result = isna_f(df) expected = df.apply(isna_f) tm.assert_frame_equal(result, expected)
def test_notna_notnull(notna_f): assert notna_f(1.0) assert not notna_f(None) assert not notna_f(np.NaN) with cf.option_context("mode.use_inf_as_na", False): assert notna_f(np.inf) assert notna_f(-np.inf) arr = np.array([1.5, np.inf, 3.5, -np.inf]) result = notna_f(arr) assert result.all() 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 with cf.option_context("mode.use_inf_as_na", False): for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries(), ]: assert isinstance(notna_f(s), Series)
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)
def test_objarr_add_invalid(self, op, box_with_array): # invalid ops box = box_with_array obj_ser = tm.makeObjectSeries() obj_ser.name = "objects" obj_ser = tm.box_expected(obj_ser, box) with pytest.raises(Exception): op(obj_ser, 1) with pytest.raises(Exception): op(obj_ser, np.array(1, dtype=np.int64))
def test_objarr_add_invalid(self, op, box_with_array): # invalid ops box = box_with_array obj_ser = tm.makeObjectSeries() obj_ser.name = "objects" obj_ser = tm.box_expected(obj_ser, box) msg = "can only concatenate str|unsupported operand type|must be str" with pytest.raises(Exception, match=msg): op(obj_ser, 1) with pytest.raises(Exception, match=msg): op(obj_ser, np.array(1, dtype=np.int64))
def test_take(self): indices = [1, 5, -2, 6, 3, -1] for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries()]: out = s.take(indices) expected = Series( data=s.values.take(indices), index=s.index.take(indices), dtype=s.dtype ) tm.assert_series_equal(out, expected) for df in [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)
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()
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)) # test object dtype