Skip to content

Commit af20c95

Browse files
authored
test: unify tests in dtypes_tests.py (#2144)
1 parent 1d8bc31 commit af20c95

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

tests/dtypes_test.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,25 @@ def test_struct_hashes() -> None:
133133
assert len({hash(tp) for tp in (dtypes)}) == 3
134134

135135

136-
@pytest.mark.skipif(PANDAS_VERSION < (2, 2), reason="old pandas")
137136
def test_2d_array(constructor: Constructor, request: pytest.FixtureRequest) -> None:
137+
version_conditions = [
138+
(PANDAS_VERSION < (2, 2), "Requires pandas 2.2+ for 2D array support"),
139+
(
140+
"pyarrow_table" in str(constructor) and PYARROW_VERSION < (14,),
141+
"PyArrow 14+ required for 2D array support",
142+
),
143+
]
144+
for condition, reason in version_conditions:
145+
if condition:
146+
pytest.skip(reason)
147+
138148
if any(x in str(constructor) for x in ("dask", "modin", "cudf", "pyspark")):
139-
request.applymarker(pytest.mark.xfail)
140-
if "pyarrow_table" in str(constructor) and PYARROW_VERSION < (14,):
141-
request.applymarker(pytest.mark.xfail)
149+
request.applymarker(
150+
pytest.mark.xfail(
151+
reason="2D array operations not supported in these backends"
152+
)
153+
)
154+
142155
data = {"a": [[[1, 2], [3, 4], [5, 6]]]}
143156
df = nw.from_native(constructor(data)).with_columns(
144157
a=nw.col("a").cast(nw.Array(nw.Int64(), (3, 2)))
@@ -150,34 +163,36 @@ def test_2d_array(constructor: Constructor, request: pytest.FixtureRequest) -> N
150163
def test_second_time_unit() -> None:
151164
s: IntoSeries = pd.Series(np.array([np.datetime64("2020-01-01", "s")]))
152165
result = nw.from_native(s, series_only=True)
153-
if PANDAS_VERSION < (2,): # pragma: no cover
154-
assert result.dtype == nw.Datetime("ns")
155-
else:
156-
assert result.dtype == nw.Datetime("s")
166+
expected_unit: Literal["ns", "us", "ms", "s"] = (
167+
"s" if PANDAS_VERSION >= (2,) else "ns"
168+
)
169+
assert result.dtype == nw.Datetime(expected_unit)
170+
157171
ts_sec = pa.timestamp("s")
158-
dur_sec = pa.duration("s")
159172
s = pa.chunked_array([pa.array([datetime(2020, 1, 1)], type=ts_sec)], type=ts_sec)
160173
result = nw.from_native(s, series_only=True)
161174
assert result.dtype == nw.Datetime("s")
175+
162176
s = pd.Series(np.array([np.timedelta64(1, "s")]))
163177
result = nw.from_native(s, series_only=True)
164-
if PANDAS_VERSION < (2,): # pragma: no cover
165-
assert result.dtype == nw.Duration("ns")
166-
else:
167-
assert result.dtype == nw.Duration("s")
178+
assert result.dtype == nw.Duration(expected_unit)
179+
180+
dur_sec = pa.duration("s")
168181
s = pa.chunked_array([pa.array([timedelta(1)], type=dur_sec)], type=dur_sec)
169182
result = nw.from_native(s, series_only=True)
170183
assert result.dtype == nw.Duration("s")
171184

172185

186+
@pytest.mark.skipif(
187+
PANDAS_VERSION >= (3,),
188+
reason="pandas 3.0+ disallows this kind of inplace modification",
189+
)
190+
@pytest.mark.skipif(
191+
PANDAS_VERSION < (1, 4),
192+
reason="pandas pre 1.4 doesn't change the type on inplace modification",
193+
)
173194
@pytest.mark.filterwarnings("ignore:Setting an item of incompatible")
174-
def test_pandas_inplace_modification_1267(request: pytest.FixtureRequest) -> None:
175-
if PANDAS_VERSION >= (3,):
176-
# pandas 3.0+ won't allow this kind of inplace modification
177-
request.applymarker(pytest.mark.xfail)
178-
if PANDAS_VERSION < (1, 4):
179-
# pandas pre 1.4 wouldn't change the type?
180-
request.applymarker(pytest.mark.xfail)
195+
def test_pandas_inplace_modification_1267() -> None:
181196
s = pd.Series([1, 2, 3])
182197
snw = nw.from_native(s, series_only=True)
183198
assert snw.dtype == nw.Int64
@@ -190,10 +205,9 @@ def test_pandas_fixed_offset_1302() -> None:
190205
pd.Series(pd.to_datetime(["2020-01-01T00:00:00.000000000+01:00"])),
191206
series_only=True,
192207
).dtype
193-
if PANDAS_VERSION >= (2,):
194-
assert result == nw.Datetime("ns", "UTC+01:00")
195-
else: # pragma: no cover
196-
assert result == nw.Datetime("ns", "pytz.FixedOffset(60)")
208+
expected_timezone = "UTC+01:00" if PANDAS_VERSION >= (2,) else "pytz.FixedOffset(60)"
209+
assert result == nw.Datetime("ns", expected_timezone)
210+
197211
if PANDAS_VERSION >= (2,):
198212
result = nw.from_native(
199213
pd.Series(
@@ -209,18 +223,21 @@ def test_pandas_fixed_offset_1302() -> None:
209223
def test_huge_int() -> None:
210224
duckdb = pytest.importorskip("duckdb")
211225
df = pl.DataFrame({"a": [1, 2, 3]})
212-
if POLARS_VERSION >= (1, 18): # pragma: no cover
226+
227+
if POLARS_VERSION >= (1, 18):
213228
result = nw.from_native(df.select(pl.col("a").cast(pl.Int128))).schema
214229
assert result["a"] == nw.Int128
215230
else: # pragma: no cover
216231
# Int128 was not available yet
217232
pass
233+
218234
rel = duckdb.sql("""
219235
select cast(a as int128) as a
220236
from df
221237
""")
222238
result = nw.from_native(rel).schema
223239
assert result["a"] == nw.Int128
240+
224241
rel = duckdb.sql("""
225242
select cast(a as uint128) as a
226243
from df
@@ -304,19 +321,15 @@ def test_dtype_is_x() -> None:
304321
assert dtype.is_nested() == (dtype in is_nested)
305322

306323

324+
@pytest.mark.skipif(POLARS_VERSION < (1, 18), reason="too old for Int128")
307325
def test_huge_int_to_native() -> None:
308326
duckdb = pytest.importorskip("duckdb")
309327
df = pl.DataFrame({"a": [1, 2, 3]})
310-
if POLARS_VERSION >= (1, 18): # pragma: no cover
311-
df_casted = (
312-
nw.from_native(df)
313-
.with_columns(a_int=nw.col("a").cast(nw.Int128()))
314-
.to_native()
315-
)
316-
assert df_casted.schema["a_int"] == pl.Int128
317-
else: # pragma: no cover
318-
# Int128 was not available yet
319-
pass
328+
df_casted = (
329+
nw.from_native(df).with_columns(a_int=nw.col("a").cast(nw.Int128())).to_native()
330+
)
331+
assert df_casted.schema["a_int"] == pl.Int128
332+
320333
rel = duckdb.sql("""
321334
select cast(a as int64) as a
322335
from df

0 commit comments

Comments
 (0)