Skip to content

Fix incorrect example in wide_to_long docstring #25736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
pytest -q --doctest-modules \
pandas/core/reshape/concat.py \
pandas/core/reshape/pivot.py \
pandas/core/reshape/melt.py \
pandas/core/reshape/reshape.py \
pandas/core/reshape/tile.py \
-k"-crosstab -pivot_table -cut"
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Reshaping
- Bug in :func:`merge` when merging by index name would sometimes result in an incorrectly numbered index (:issue:`24212`)
- :func:`to_records` now accepts dtypes to its `column_dtypes` parameter (:issue:`24895`)
- Bug in :func:`concat` where order of ``OrderedDict`` (and ``dict`` in Python 3.6+) is not respected, when passed in as ``objs`` argument (:issue:`21510`)

- Bug in last example in docstring of :func:`wide_to_long` has been fixed (:issue:`25733`)

Sparse
^^^^^^
Expand Down
20 changes: 8 additions & 12 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
With multiple id columns

>>> df = pd.DataFrame({
... 'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
... 'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],
... 'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
... 'ht1': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],
... 'ht2': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
... })
Expand Down Expand Up @@ -333,14 +333,10 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
... 'X' : np.random.randint(3, size=3)})
>>> df['id'] = df.index
>>> df # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
A(quarterly)-2010 A(quarterly)-2011 B(quarterly)-2010 ...
0 0.548814 0.544883 0.437587 ...
1 0.715189 0.423655 0.891773 ...
2 0.602763 0.645894 0.963663 ...
X id
0 0 0
1 1 1
2 1 2
A(quarterly)-2010 A(quarterly)-2011 B(quarterly)-2010 ... X id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the output too wide to include all the columns without any ellipses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @TomAugspurger the line is too wide to include all columns, so deleted only "B(quarterly)-2011" column (88 > 79 chars on pycodestyle and flake8)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, that's a little unfortunate as all the columns are important :/

Well, for now, let's use ellipses. FYI, your spacing is just slightly off, so it failed again :)

diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py
index cec4c609c..df144db50 100644
--- a/pandas/core/reshape/melt.py
+++ b/pandas/core/reshape/melt.py
@@ -334,9 +334,9 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
     >>> df['id'] = df.index
     >>> df # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
        A(quarterly)-2010  A(quarterly)-2011  B(quarterly)-2010  ...  X  id
-    0           0.548814           0.544883           0.437587  ...  0  0
-    1           0.715189           0.423655           0.891773  ...  1  1
-    2           0.602763           0.645894           0.963663  ...  2  2
+    0           0.548814           0.544883           0.437587  ...  0   0
+    1           0.715189           0.423655           0.891773  ...  1   1
+    2           0.602763           0.645894           0.963663  ...  1   2
 
     >>> pd.wide_to_long(df, ['A(quarterly)', 'B(quarterly)'], i='id',
     ...                 j='year', sep='-')

I think you need an extra space so that the values in id are under the d.

0 0.548814 0.544883 0.437587 ... 0 0
1 0.715189 0.423655 0.891773 ... 1 1
2 0.602763 0.645894 0.963663 ... 2 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can delete this line I think.

>>> pd.wide_to_long(df, ['A(quarterly)', 'B(quarterly)'], i='id',
... j='year', sep='-')
Expand Down Expand Up @@ -368,8 +364,8 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
have non-integers as suffixes.

>>> df = pd.DataFrame({
... 'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
... 'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],
... 'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
... 'ht_one': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],
... 'ht_two': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
... })
Expand All @@ -386,9 +382,9 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
8 3 3 2.1 2.9

>>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age',
sep='_', suffix='\w')
... sep='_', suffix='\w+')
>>> l
... # doctest: +NORMALIZE_WHITESPACE
... # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
ht
famid birth age
1 1 one 2.8
Expand Down