Skip to content

Commit 9217d46

Browse files
authored
Merge branch 'main' into cow_replace
2 parents 16dbe9a + ce9e3d6 commit 9217d46

File tree

6 files changed

+90
-33
lines changed

6 files changed

+90
-33
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,3 @@ repos:
443443
types: [python]
444444
files: ^pandas/tests
445445
language: python
446-
exclude: |
447-
(?x)
448-
^pandas/tests/generic/test_generic.py # GH50380

pandas/_testing/asserters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,9 @@ def assert_indexing_slices_equivalent(ser: Series, l_slc: slice, i_slc: slice) -
13411341
assert_series_equal(ser[l_slc], expected)
13421342

13431343

1344-
def assert_metadata_equivalent(left, right) -> None:
1344+
def assert_metadata_equivalent(
1345+
left: DataFrame | Series, right: DataFrame | Series | None = None
1346+
) -> None:
13451347
"""
13461348
Check that ._metadata attributes are equivalent.
13471349
"""

pandas/tests/copy_view/test_methods.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Series,
1010
Timestamp,
1111
date_range,
12+
period_range,
1213
)
1314
import pandas._testing as tm
1415
from pandas.tests.copy_view.util import get_array
@@ -53,6 +54,78 @@ def test_copy_shallow(using_copy_on_write):
5354
assert np.shares_memory(get_array(df_copy, "a"), get_array(df, "a"))
5455

5556

57+
@pytest.mark.parametrize("copy", [True, None, False])
58+
@pytest.mark.parametrize(
59+
"method",
60+
[
61+
lambda df, copy: df.rename(columns=str.lower, copy=copy),
62+
lambda df, copy: df.reindex(columns=["a", "c"], copy=copy),
63+
lambda df, copy: df.reindex_like(df, copy=copy),
64+
lambda df, copy: df.set_axis(["a", "b", "c"], axis="index", copy=copy),
65+
lambda df, copy: df.rename_axis(index="test", copy=copy),
66+
lambda df, copy: df.rename_axis(columns="test", copy=copy),
67+
# lambda df, copy: df.astype({'b': 'int64'}, copy=copy),
68+
# lambda df, copy: df.swaplevel(0, 0, copy=copy),
69+
lambda df, copy: df.swapaxes(0, 0, copy=copy),
70+
lambda df, copy: df.truncate(0, 5, copy=copy),
71+
# lambda df, copy: df.infer_objects(copy=copy)
72+
lambda df, copy: df.to_timestamp(copy=copy),
73+
lambda df, copy: df.to_period(freq="D", copy=copy),
74+
lambda df, copy: df.tz_localize("US/Central", copy=copy),
75+
lambda df, copy: df.tz_convert("US/Central", copy=copy),
76+
lambda df, copy: df.set_flags(allows_duplicate_labels=False, copy=copy),
77+
],
78+
ids=[
79+
"rename",
80+
"reindex",
81+
"reindex_like",
82+
"set_axis",
83+
"rename_axis0",
84+
"rename_axis1",
85+
# "astype", # CoW not yet implemented
86+
# "swaplevel", # only series
87+
"swapaxes",
88+
"truncate",
89+
# "infer_objects", # CoW not yet implemented
90+
"to_timestamp",
91+
"to_period",
92+
"tz_localize",
93+
"tz_convert",
94+
"set_flags",
95+
],
96+
)
97+
def test_methods_copy_keyword(
98+
request, method, copy, using_copy_on_write, using_array_manager
99+
):
100+
index = None
101+
if "to_timestamp" in request.node.callspec.id:
102+
index = period_range("2012-01-01", freq="D", periods=3)
103+
elif "to_period" in request.node.callspec.id:
104+
index = date_range("2012-01-01", freq="D", periods=3)
105+
elif "tz_localize" in request.node.callspec.id:
106+
index = date_range("2012-01-01", freq="D", periods=3)
107+
elif "tz_convert" in request.node.callspec.id:
108+
index = date_range("2012-01-01", freq="D", periods=3, tz="Europe/Brussels")
109+
110+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=index)
111+
df2 = method(df, copy=copy)
112+
113+
share_memory = (using_copy_on_write and copy is not True) or copy is False
114+
115+
if request.node.callspec.id.startswith("reindex-"):
116+
# TODO copy=False without CoW still returns a copy in this case
117+
if not using_copy_on_write and not using_array_manager and copy is False:
118+
share_memory = False
119+
# TODO copy=True with CoW still returns a view
120+
if using_copy_on_write:
121+
share_memory = True
122+
123+
if share_memory:
124+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
125+
else:
126+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
127+
128+
56129
# -----------------------------------------------------------------------------
57130
# DataFrame methods returning new DataFrame using shallow copy
58131

pandas/tests/generic/test_generic.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def construct(box, shape, value=None, dtype=None, **kwargs):
5050
return box(arr, dtype=dtype, **kwargs)
5151

5252

53-
class Generic:
53+
class TestGeneric:
5454
@pytest.mark.parametrize(
5555
"func",
5656
[
@@ -66,7 +66,7 @@ def test_rename(self, frame_or_series, func):
6666

6767
for axis in frame_or_series._AXIS_ORDERS:
6868
kwargs = {axis: idx}
69-
obj = construct(4, **kwargs)
69+
obj = construct(frame_or_series, 4, **kwargs)
7070

7171
# rename a single axis
7272
result = obj.rename(**{axis: func})
@@ -83,21 +83,22 @@ def test_get_numeric_data(self, frame_or_series):
8383
}
8484

8585
# get the numeric data
86-
o = construct(n, **kwargs)
86+
o = construct(frame_or_series, n, **kwargs)
8787
result = o._get_numeric_data()
8888
tm.assert_equal(result, o)
8989

9090
# non-inclusion
9191
result = o._get_bool_data()
92-
expected = construct(n, value="empty", **kwargs)
92+
expected = construct(frame_or_series, n, value="empty", **kwargs)
9393
if isinstance(o, DataFrame):
9494
# preserve columns dtype
9595
expected.columns = o.columns[:0]
96-
tm.assert_equal(result, expected)
96+
# https://github.com/pandas-dev/pandas/issues/50862
97+
tm.assert_equal(result.reset_index(drop=True), expected)
9798

9899
# get the bool data
99100
arr = np.array([True, True, False, True])
100-
o = construct(n, value=arr, **kwargs)
101+
o = construct(frame_or_series, n, value=arr, **kwargs)
101102
result = o._get_numeric_data()
102103
tm.assert_equal(result, o)
103104

@@ -160,7 +161,7 @@ def f(dtype):
160161

161162
msg = (
162163
"compound dtypes are not implemented "
163-
f"in the {frame_or_series.__name__} frame_or_series"
164+
f"in the {frame_or_series.__name__} constructor"
164165
)
165166

166167
with pytest.raises(NotImplementedError, match=msg):
@@ -257,7 +258,7 @@ def test_api_compat(self, func, frame_or_series):
257258
# GH 12021
258259
# compat for __name__, __qualname__
259260

260-
obj = (frame_or_series, 5)
261+
obj = construct(frame_or_series, 5)
261262
f = getattr(obj, func)
262263
assert f.__name__ == func
263264
assert f.__qualname__.endswith(func)

pandas/tests/io/test_fsspec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ def test_read_csv(cleared_fs, df1):
5050

5151

5252
def test_reasonable_error(monkeypatch, cleared_fs):
53-
from fsspec import registry
5453
from fsspec.registry import known_implementations
5554

56-
registry.target.clear()
5755
with pytest.raises(ValueError, match="nosuchprotocol"):
5856
read_csv("nosuchprotocol://test/test.csv")
5957
err_msg = "test error message"

pandas/tests/io/test_gcs.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@
2222
@pytest.fixture
2323
def gcs_buffer(monkeypatch):
2424
"""Emulate GCS using a binary buffer."""
25-
from fsspec import (
26-
AbstractFileSystem,
27-
registry,
28-
)
29-
30-
registry.target.clear() # remove state
25+
import fsspec
3126

3227
gcs_buffer = BytesIO()
3328
gcs_buffer.close = lambda: True
3429

35-
class MockGCSFileSystem(AbstractFileSystem):
30+
class MockGCSFileSystem(fsspec.AbstractFileSystem):
3631
@staticmethod
3732
def open(*args, **kwargs):
3833
gcs_buffer.seek(0)
@@ -42,7 +37,8 @@ def ls(self, path, **kwargs):
4237
# needed for pyarrow
4338
return [{"name": path, "type": "file"}]
4439

45-
monkeypatch.setattr("gcsfs.GCSFileSystem", MockGCSFileSystem)
40+
# Overwrites the default implementation from gcsfs to our mock class
41+
fsspec.register_implementation("gs", MockGCSFileSystem, clobber=True)
4642

4743
return gcs_buffer
4844

@@ -55,9 +51,6 @@ def test_to_read_gcs(gcs_buffer, format):
5551
5652
GH 33987
5753
"""
58-
from fsspec import registry
59-
60-
registry.target.clear() # remove state
6154

6255
df1 = DataFrame(
6356
{
@@ -132,9 +125,6 @@ def test_to_csv_compression_encoding_gcs(gcs_buffer, compression_only, encoding)
132125
GH 35677 (to_csv, compression), GH 26124 (to_csv, encoding), and
133126
GH 32392 (read_csv, encoding)
134127
"""
135-
from fsspec import registry
136-
137-
registry.target.clear() # remove state
138128
df = tm.makeDataFrame()
139129

140130
# reference of compressed and encoded file
@@ -174,12 +164,8 @@ def test_to_csv_compression_encoding_gcs(gcs_buffer, compression_only, encoding)
174164
@td.skip_if_no("gcsfs")
175165
def test_to_parquet_gcs_new_file(monkeypatch, tmpdir):
176166
"""Regression test for writing to a not-yet-existent GCS Parquet file."""
177-
from fsspec import (
178-
AbstractFileSystem,
179-
registry,
180-
)
167+
from fsspec import AbstractFileSystem
181168

182-
registry.target.clear() # remove state
183169
df1 = DataFrame(
184170
{
185171
"int": [1, 3],

0 commit comments

Comments
 (0)