Skip to content

Commit 1760439

Browse files
authored
Merge branch 'main' into Series-dt-isocalendar-docs
2 parents e8314d2 + 361021b commit 1760439

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

ci/deps/actions-38-downstream_compat.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ dependencies:
6060
- cftime
6161
- dask
6262
- ipython
63-
- geopandas
63+
- geopandas-base
6464
- seaborn
6565
- scikit-learn
6666
- statsmodels

doc/source/whatsnew/v1.5.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Other enhancements
9292
- :class:`Series` and :class:`DataFrame` with ``IntegerDtype`` now supports bitwise operations (:issue:`34463`)
9393
- Add ``milliseconds`` field support for :class:`~pandas.DateOffset` (:issue:`43371`)
9494
- :meth:`DataFrame.reset_index` now accepts a ``names`` argument which renames the index names (:issue:`6878`)
95+
- :meth:`pd.concat` now raises when ``levels`` is given but ``keys`` is None (:issue:`46653`)
96+
- :meth:`pd.concat` now raises when ``levels`` contains duplicate values (:issue:`46653`)
97+
-
9598

9699
.. ---------------------------------------------------------------------------
97100
.. _whatsnew_150.notable_bug_fixes:

pandas/core/reshape/concat.py

+6
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,8 @@ def _get_concat_axis(self) -> Index:
668668
return idx
669669

670670
if self.keys is None:
671+
if self.levels is not None:
672+
raise ValueError("levels supported only when keys is not None")
671673
concat_axis = _concat_indexes(indexes)
672674
else:
673675
concat_axis = _make_concat_multiindex(
@@ -712,6 +714,10 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde
712714
else:
713715
levels = [ensure_index(x) for x in levels]
714716

717+
for level in levels:
718+
if not level.is_unique:
719+
raise ValueError(f"Level values not unique: {level.tolist()}")
720+
715721
if not all_indexes_same(indexes) or not all(level.is_unique for level in levels):
716722
codes_list = []
717723

pandas/tests/reshape/concat/test_index.py

+16
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,19 @@ def test_concat_with_key_not_unique(self):
371371
out_b = df_b.loc[("x", 0), :]
372372

373373
tm.assert_frame_equal(out_a, out_b)
374+
375+
def test_concat_with_duplicated_levels(self):
376+
# keyword levels should be unique
377+
df1 = DataFrame({"A": [1]}, index=["x"])
378+
df2 = DataFrame({"A": [1]}, index=["y"])
379+
msg = r"Level values not unique: \['x', 'y', 'y'\]"
380+
with pytest.raises(ValueError, match=msg):
381+
concat([df1, df2], keys=["x", "y"], levels=[["x", "y", "y"]])
382+
383+
@pytest.mark.parametrize("levels", [[["x", "y"]], [["x", "y", "y"]]])
384+
def test_concat_with_levels_with_none_keys(self, levels):
385+
df1 = DataFrame({"A": [1]}, index=["x"])
386+
df2 = DataFrame({"A": [1]}, index=["y"])
387+
msg = "levels supported only when keys is not None"
388+
with pytest.raises(ValueError, match=msg):
389+
concat([df1, df2], levels=levels)

pandas/tests/test_downstream.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import numpy as np
99
import pytest
1010

11-
from pandas.compat import is_platform_windows
1211
import pandas.util._test_decorators as td
1312

1413
import pandas as pd
@@ -224,20 +223,13 @@ def test_pandas_datareader():
224223
pandas_datareader.DataReader("F", "quandl", "2017-01-01", "2017-02-01")
225224

226225

227-
# importing from pandas, Cython import warning
228-
@pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
229-
@pytest.mark.xfail(
230-
is_platform_windows(),
231-
raises=ImportError,
232-
reason="ImportError: the 'read_file' function requires the 'fiona' package, "
233-
"but it is not installed or does not import correctly",
234-
strict=False,
235-
)
236226
def test_geopandas():
237227

238228
geopandas = import_module("geopandas")
239-
fp = geopandas.datasets.get_path("naturalearth_lowres")
240-
assert geopandas.read_file(fp) is not None
229+
gdf = geopandas.GeoDataFrame(
230+
{"col": [1, 2, 3], "geometry": geopandas.points_from_xy([1, 2, 3], [1, 2, 3])}
231+
)
232+
assert gdf[["col", "geometry"]].geometry.x.equals(Series([1.0, 2.0, 3.0]))
241233

242234

243235
# Cython import warning

0 commit comments

Comments
 (0)