Skip to content

Commit 8995d94

Browse files
committed
Merge remote-tracking branch 'upstream/master' into post_numpy_bump
2 parents d854b29 + e405a10 commit 8995d94

File tree

15 files changed

+1866
-1513
lines changed

15 files changed

+1866
-1513
lines changed

doc/source/categorical.rst

Lines changed: 111 additions & 97 deletions
Large diffs are not rendered by default.

doc/source/dsintro.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ support the multi-dimensional analysis that is one of ``Panel`` s main use cases
10281028
.. ipython:: python
10291029
:okwarning:
10301030
1031+
import pandas.util.testing as tm
10311032
p = tm.makePanel()
10321033
p
10331034

doc/source/whatsnew/v0.24.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,7 @@ Reshaping
14421442
- Bug in :func:`pandas.concat` when concatenating a multicolumn DataFrame with tz-aware data against a DataFrame with a different number of columns (:issue:`22796`)
14431443
- Bug in :func:`merge_asof` where confusing error message raised when attempting to merge with missing values (:issue:`23189`)
14441444
- Bug in :meth:`DataFrame.nsmallest` and :meth:`DataFrame.nlargest` for dataframes that have a :class:`MultiIndex` for columns (:issue:`23033`).
1445+
- Bug in :func:`pandas.melt` when passing column names that are not present in ``DataFrame`` (:issue:`23575`)
14451446
- Bug in :meth:`DataFrame.append` with a :class:`Series` with a dateutil timezone would raise a ``TypeError`` (:issue:`23682`)
14461447
- Bug in ``Series`` construction when passing no data and ``dtype=str`` (:issue:`22477`)
14471448

pandas/core/reshape/melt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas import compat
1414
from pandas.core.arrays import Categorical
1515
from pandas.core.frame import _shared_docs
16+
from pandas.core.indexes.base import Index
1617
from pandas.core.reshape.concat import concat
1718
from pandas.core.tools.numeric import to_numeric
1819

@@ -24,6 +25,12 @@
2425
def melt(frame, id_vars=None, value_vars=None, var_name=None,
2526
value_name='value', col_level=None):
2627
# TODO: what about the existing index?
28+
# If multiindex, gather names of columns on all level for checking presence
29+
# of `id_vars` and `value_vars`
30+
if isinstance(frame.columns, ABCMultiIndex):
31+
cols = [x for c in frame.columns for x in c]
32+
else:
33+
cols = list(frame.columns)
2734
if id_vars is not None:
2835
if not is_list_like(id_vars):
2936
id_vars = [id_vars]
@@ -32,7 +39,13 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None,
3239
raise ValueError('id_vars must be a list of tuples when columns'
3340
' are a MultiIndex')
3441
else:
42+
# Check that `id_vars` are in frame
3543
id_vars = list(id_vars)
44+
missing = Index(np.ravel(id_vars)).difference(cols)
45+
if not missing.empty:
46+
raise KeyError("The following 'id_vars' are not present"
47+
" in the DataFrame: {missing}"
48+
"".format(missing=list(missing)))
3649
else:
3750
id_vars = []
3851

@@ -45,6 +58,12 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None,
4558
' columns are a MultiIndex')
4659
else:
4760
value_vars = list(value_vars)
61+
# Check that `value_vars` are in frame
62+
missing = Index(np.ravel(value_vars)).difference(cols)
63+
if not missing.empty:
64+
raise KeyError("The following 'value_vars' are not present in"
65+
" the DataFrame: {missing}"
66+
"".format(missing=list(missing)))
4867
frame = frame.loc[:, id_vars + value_vars]
4968
else:
5069
frame = frame.copy()

pandas/tests/io/parser/multithread.py

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)