Skip to content

TST/REF: method-specific files for lookup, get_value, set_value #37325

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

Merged
merged 2 commits into from
Oct 22, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pandas/tests/frame/indexing/test_get_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from pandas import DataFrame, MultiIndex


class TestGetValue:
def test_get_set_value_no_partial_indexing(self):
# partial w/ MultiIndex raise exception
index = MultiIndex.from_tuples([(0, 1), (0, 2), (1, 1), (1, 2)])
df = DataFrame(index=index, columns=range(4))
with pytest.raises(KeyError, match=r"^0$"):
df._get_value(0, 1)

def test_get_value(self, float_frame):
for idx in float_frame.index:
for col in float_frame.columns:
result = float_frame._get_value(idx, col)
expected = float_frame[col][idx]
assert result == expected
132 changes: 1 addition & 131 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pandas._libs import iNaT

from pandas.core.dtypes.common import is_float_dtype, is_integer
from pandas.core.dtypes.common import is_integer

import pandas as pd
from pandas import (
Expand Down Expand Up @@ -1327,120 +1327,6 @@ def test_getitem_list_duplicates(self):
expected = df.iloc[:, 2:]
tm.assert_frame_equal(result, expected)

def test_get_value(self, float_frame):
for idx in float_frame.index:
for col in float_frame.columns:
result = float_frame._get_value(idx, col)
expected = float_frame[col][idx]
assert result == expected

def test_lookup_float(self, float_frame):
df = float_frame
rows = list(df.index) * len(df.columns)
cols = list(df.columns) * len(df.index)
with tm.assert_produces_warning(FutureWarning):
result = df.lookup(rows, cols)

expected = np.array([df.loc[r, c] for r, c in zip(rows, cols)])
tm.assert_numpy_array_equal(result, expected)

def test_lookup_mixed(self, float_string_frame):
df = float_string_frame
rows = list(df.index) * len(df.columns)
cols = list(df.columns) * len(df.index)
with tm.assert_produces_warning(FutureWarning):
result = df.lookup(rows, cols)

expected = np.array(
[df.loc[r, c] for r, c in zip(rows, cols)], dtype=np.object_
)
tm.assert_almost_equal(result, expected)

def test_lookup_bool(self):
df = DataFrame(
{
"label": ["a", "b", "a", "c"],
"mask_a": [True, True, False, True],
"mask_b": [True, False, False, False],
"mask_c": [False, True, False, True],
}
)
with tm.assert_produces_warning(FutureWarning):
df["mask"] = df.lookup(df.index, "mask_" + df["label"])

exp_mask = np.array(
[df.loc[r, c] for r, c in zip(df.index, "mask_" + df["label"])]
)

tm.assert_series_equal(df["mask"], Series(exp_mask, name="mask"))
assert df["mask"].dtype == np.bool_

def test_lookup_raises(self, float_frame):
with pytest.raises(KeyError, match="'One or more row labels was not found'"):
with tm.assert_produces_warning(FutureWarning):
float_frame.lookup(["xyz"], ["A"])

with pytest.raises(KeyError, match="'One or more column labels was not found'"):
with tm.assert_produces_warning(FutureWarning):
float_frame.lookup([float_frame.index[0]], ["xyz"])

with pytest.raises(ValueError, match="same size"):
with tm.assert_produces_warning(FutureWarning):
float_frame.lookup(["a", "b", "c"], ["a"])

def test_lookup_requires_unique_axes(self):
# GH#33041 raise with a helpful error message
df = DataFrame(np.random.randn(6).reshape(3, 2), columns=["A", "A"])

rows = [0, 1]
cols = ["A", "A"]

# homogeneous-dtype case
with pytest.raises(ValueError, match="requires unique index and columns"):
with tm.assert_produces_warning(FutureWarning):
df.lookup(rows, cols)
with pytest.raises(ValueError, match="requires unique index and columns"):
with tm.assert_produces_warning(FutureWarning):
df.T.lookup(cols, rows)

# heterogeneous dtype
df["B"] = 0
with pytest.raises(ValueError, match="requires unique index and columns"):
with tm.assert_produces_warning(FutureWarning):
df.lookup(rows, cols)

def test_set_value(self, float_frame):
for idx in float_frame.index:
for col in float_frame.columns:
float_frame._set_value(idx, col, 1)
assert float_frame[col][idx] == 1

def test_set_value_resize(self, float_frame):

res = float_frame._set_value("foobar", "B", 0)
assert res is None
assert float_frame.index[-1] == "foobar"
assert float_frame._get_value("foobar", "B") == 0

float_frame.loc["foobar", "qux"] = 0
assert float_frame._get_value("foobar", "qux") == 0

res = float_frame.copy()
res._set_value("foobar", "baz", "sam")
assert res["baz"].dtype == np.object_

res = float_frame.copy()
res._set_value("foobar", "baz", True)
assert res["baz"].dtype == np.object_

res = float_frame.copy()
res._set_value("foobar", "baz", 5)
assert is_float_dtype(res["baz"])
assert isna(res["baz"].drop(["foobar"])).all()
msg = "could not convert string to float: 'sam'"
with pytest.raises(ValueError, match=msg):
res._set_value("foobar", "baz", "sam")

def test_reindex_with_multi_index(self):
# https://github.com/pandas-dev/pandas/issues/29896
# tests for reindexing a multi-indexed DataFrame with a new MultiIndex
Expand Down Expand Up @@ -1542,13 +1428,6 @@ def test_set_value_with_index_dtype_change(self):
assert list(df.index) == list(df_orig.index) + ["C"]
assert list(df.columns) == list(df_orig.columns) + ["D"]

def test_get_set_value_no_partial_indexing(self):
# partial w/ MultiIndex raise exception
index = MultiIndex.from_tuples([(0, 1), (0, 2), (1, 1), (1, 2)])
df = DataFrame(index=index, columns=range(4))
with pytest.raises(KeyError, match=r"^0$"):
df._get_value(0, 1)

# TODO: rename? remove?
def test_single_element_ix_dont_upcast(self, float_frame):
float_frame["E"] = 1
Expand Down Expand Up @@ -2251,12 +2130,3 @@ def test_object_casting_indexing_wraps_datetimelike():
assert blk.dtype == "m8[ns]" # we got the right block
val = blk.iget((0, 0))
assert isinstance(val, pd.Timedelta)


def test_lookup_deprecated():
# GH18262
df = DataFrame(
{"col": ["A", "A", "B", "B"], "A": [80, 23, np.nan, 22], "B": [80, 55, 76, 67]}
)
with tm.assert_produces_warning(FutureWarning):
df.lookup(df.index, df["col"])
91 changes: 91 additions & 0 deletions pandas/tests/frame/indexing/test_lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import numpy as np
import pytest

from pandas import DataFrame, Series
import pandas._testing as tm


class TestLookup:
def test_lookup_float(self, float_frame):
df = float_frame
rows = list(df.index) * len(df.columns)
cols = list(df.columns) * len(df.index)
with tm.assert_produces_warning(FutureWarning):
result = df.lookup(rows, cols)

expected = np.array([df.loc[r, c] for r, c in zip(rows, cols)])
tm.assert_numpy_array_equal(result, expected)

def test_lookup_mixed(self, float_string_frame):
df = float_string_frame
rows = list(df.index) * len(df.columns)
cols = list(df.columns) * len(df.index)
with tm.assert_produces_warning(FutureWarning):
result = df.lookup(rows, cols)

expected = np.array(
[df.loc[r, c] for r, c in zip(rows, cols)], dtype=np.object_
)
tm.assert_almost_equal(result, expected)

def test_lookup_bool(self):
df = DataFrame(
{
"label": ["a", "b", "a", "c"],
"mask_a": [True, True, False, True],
"mask_b": [True, False, False, False],
"mask_c": [False, True, False, True],
}
)
with tm.assert_produces_warning(FutureWarning):
df["mask"] = df.lookup(df.index, "mask_" + df["label"])

exp_mask = np.array(
[df.loc[r, c] for r, c in zip(df.index, "mask_" + df["label"])]
)

tm.assert_series_equal(df["mask"], Series(exp_mask, name="mask"))
assert df["mask"].dtype == np.bool_

def test_lookup_raises(self, float_frame):
with pytest.raises(KeyError, match="'One or more row labels was not found'"):
with tm.assert_produces_warning(FutureWarning):
float_frame.lookup(["xyz"], ["A"])

with pytest.raises(KeyError, match="'One or more column labels was not found'"):
with tm.assert_produces_warning(FutureWarning):
float_frame.lookup([float_frame.index[0]], ["xyz"])

with pytest.raises(ValueError, match="same size"):
with tm.assert_produces_warning(FutureWarning):
float_frame.lookup(["a", "b", "c"], ["a"])

def test_lookup_requires_unique_axes(self):
# GH#33041 raise with a helpful error message
df = DataFrame(np.random.randn(6).reshape(3, 2), columns=["A", "A"])

rows = [0, 1]
cols = ["A", "A"]

# homogeneous-dtype case
with pytest.raises(ValueError, match="requires unique index and columns"):
with tm.assert_produces_warning(FutureWarning):
df.lookup(rows, cols)
with pytest.raises(ValueError, match="requires unique index and columns"):
with tm.assert_produces_warning(FutureWarning):
df.T.lookup(cols, rows)

# heterogeneous dtype
df["B"] = 0
with pytest.raises(ValueError, match="requires unique index and columns"):
with tm.assert_produces_warning(FutureWarning):
df.lookup(rows, cols)


def test_lookup_deprecated():
# GH#18262
df = DataFrame(
{"col": ["A", "A", "B", "B"], "A": [80, 23, np.nan, 22], "B": [80, 55, 76, 67]}
)
with tm.assert_produces_warning(FutureWarning):
df.lookup(df.index, df["col"])
40 changes: 40 additions & 0 deletions pandas/tests/frame/indexing/test_set_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
import pytest

from pandas.core.dtypes.common import is_float_dtype

from pandas import isna


class TestSetValue:
def test_set_value(self, float_frame):
for idx in float_frame.index:
for col in float_frame.columns:
float_frame._set_value(idx, col, 1)
assert float_frame[col][idx] == 1

def test_set_value_resize(self, float_frame):

res = float_frame._set_value("foobar", "B", 0)
assert res is None
assert float_frame.index[-1] == "foobar"
assert float_frame._get_value("foobar", "B") == 0

float_frame.loc["foobar", "qux"] = 0
assert float_frame._get_value("foobar", "qux") == 0

res = float_frame.copy()
res._set_value("foobar", "baz", "sam")
assert res["baz"].dtype == np.object_

res = float_frame.copy()
res._set_value("foobar", "baz", True)
assert res["baz"].dtype == np.object_

res = float_frame.copy()
res._set_value("foobar", "baz", 5)
assert is_float_dtype(res["baz"])
assert isna(res["baz"].drop(["foobar"])).all()
msg = "could not convert string to float: 'sam'"
with pytest.raises(ValueError, match=msg):
res._set_value("foobar", "baz", "sam")
15 changes: 0 additions & 15 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ def test_dti_reset_index_round_trip():
assert df.reset_index()["Date"][0] == stamp


def test_series_set_value():
# #1561

dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)]
index = DatetimeIndex(dates)

s = Series(dtype=object)
s._set_value(dates[0], 1.0)
s._set_value(dates[1], np.nan)

expected = Series([1.0, np.nan], index=index)

tm.assert_series_equal(s, expected)


@pytest.mark.slow
def test_slice_locs_indexerror():
times = [datetime(2000, 1, 1) + timedelta(minutes=i * 10) for i in range(100000)]
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/indexing/test_set_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from datetime import datetime

import numpy as np

from pandas import DatetimeIndex, Series
import pandas._testing as tm


def test_series_set_value():
# GH#1561

dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)]
index = DatetimeIndex(dates)

s = Series(dtype=object)
s._set_value(dates[0], 1.0)
s._set_value(dates[1], np.nan)

expected = Series([1.0, np.nan], index=index)

tm.assert_series_equal(s, expected)