Skip to content

Commit 1bf35c9

Browse files
authored
FIX-#2408: Fix read_csv and read_table args when used inside a decora… (#2486)
Signed-off-by: Weiwen Gu <[email protected]>
1 parent ebbb6b2 commit 1bf35c9

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

modin/pandas/io.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,15 @@ def parser_func(
103103
memory_map=False,
104104
float_precision=None,
105105
):
106-
_, _, _, kwargs = inspect.getargvalues(inspect.currentframe())
107-
if kwargs.get("sep", sep) is False:
108-
kwargs["sep"] = "\t"
106+
# ISSUE #2408: parse parameter shared with pandas read_csv and read_table and update with provided args
107+
_pd_read_csv_signature = {
108+
val.name for val in inspect.signature(pandas.read_csv).parameters.values()
109+
}
110+
_, _, _, f_locals = inspect.getargvalues(inspect.currentframe())
111+
if f_locals.get("sep", sep) is False:
112+
f_locals["sep"] = "\t"
113+
114+
kwargs = {k: v for k, v in f_locals.items() if k in _pd_read_csv_signature}
109115
return _read(**kwargs)
110116

111117
return parser_func

modin/pandas/test/test_io.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
IO_OPS_DATA_DIR,
4444
io_ops_bad_exc,
4545
eval_io_from_str,
46+
dummy_decorator,
4647
)
4748

4849
from modin.config import Engine, Backend, IsExperimental
@@ -1460,6 +1461,28 @@ def test_from_sas():
14601461
df_equals(modin_df, pandas_df)
14611462

14621463

1464+
def test_from_csv_within_decorator(make_csv_file):
1465+
make_csv_file()
1466+
1467+
@dummy_decorator()
1468+
def wrapped_read_csv(file, method):
1469+
if method == "pandas":
1470+
return pandas.read_csv(file)
1471+
1472+
if method == "modin":
1473+
return pd.read_csv(file)
1474+
1475+
pandas_df = wrapped_read_csv(TEST_CSV_FILENAME, method="pandas")
1476+
modin_df = wrapped_read_csv(TEST_CSV_FILENAME, method="modin")
1477+
1478+
df_equals(modin_df, pandas_df)
1479+
1480+
pandas_df = wrapped_read_csv(Path(TEST_CSV_FILENAME), method="pandas")
1481+
modin_df = wrapped_read_csv(Path(TEST_CSV_FILENAME), method="modin")
1482+
1483+
df_equals(modin_df, pandas_df)
1484+
1485+
14631486
@pytest.mark.parametrize("nrows", [35, None])
14641487
def test_from_csv_sep_none(make_csv_file, nrows):
14651488
make_csv_file()
@@ -1642,6 +1665,28 @@ def test_from_table(make_csv_file):
16421665
df_equals(modin_df, pandas_df)
16431666

16441667

1668+
def test_from_table_within_decorator(make_csv_file):
1669+
make_csv_file(delimiter="\t")
1670+
1671+
@dummy_decorator()
1672+
def wrapped_read_table(file, method):
1673+
if method == "pandas":
1674+
return pandas.read_table(file)
1675+
1676+
if method == "modin":
1677+
return pd.read_table(file)
1678+
1679+
pandas_df = wrapped_read_table(TEST_CSV_FILENAME, method="pandas")
1680+
modin_df = wrapped_read_table(TEST_CSV_FILENAME, method="modin")
1681+
1682+
df_equals(modin_df, pandas_df)
1683+
1684+
pandas_df = wrapped_read_table(Path(TEST_CSV_FILENAME), method="pandas")
1685+
modin_df = wrapped_read_table(Path(TEST_CSV_FILENAME), method="modin")
1686+
1687+
df_equals(modin_df, pandas_df)
1688+
1689+
16451690
@pytest.mark.skipif(Engine.get() == "Python", reason="Using pandas implementation")
16461691
def test_from_csv_s3(make_csv_file):
16471692
dataset_url = "s3://noaa-ghcn-pds/csv/1788.csv"

modin/pandas/test/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,18 @@ def check(*a, **kw):
10451045
), f"Unexpected open handles left for: {', '.join(item[0] for item in leaks)}"
10461046

10471047
return check
1048+
1049+
1050+
def dummy_decorator():
1051+
"""A problematic decorator that does not use `functools.wraps`. This introduces unwanted local variables for
1052+
inspect.currentframe. This decorator is used in test_io to test `read_csv` and `read_table`
1053+
"""
1054+
1055+
def wrapper(method):
1056+
def wrapped_function(self, *args, **kwargs):
1057+
result = method(self, *args, **kwargs)
1058+
return result
1059+
1060+
return wrapped_function
1061+
1062+
return wrapper

0 commit comments

Comments
 (0)