Skip to content

ENH: Index optional pivot #10705

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

Merged
merged 1 commit into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Other enhancements

- ``pd.merge`` will now allow duplicate column names if they are not merged upon (:issue:`10639`).

- ``pd.pivot`` will now allow passing index as ``None`` (:issue:`3962`).

.. _whatsnew_0170.api:

.. _whatsnew_0170.api_breaking:
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3460,8 +3460,9 @@ def pivot(self, index=None, columns=None, values=None):

Parameters
----------
index : string or object
Column name to use to make new frame's index
index : string or object, optional
Column name to use to make new frame's index. If None, uses
existing index.
columns : string or object
Column name to use to make new frame's columns
values : string or object, optional
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,17 @@ def pivot(self, index=None, columns=None, values=None):
See DataFrame.pivot
"""
if values is None:
indexed = self.set_index([index, columns])
cols = [columns] if index is None else [index, columns]
append = index is None
indexed = self.set_index(cols, append=append)
return indexed.unstack(columns)
else:
if index is None:
index = self.index
else:
index = self[index]
indexed = Series(self[values].values,
index=MultiIndex.from_arrays([self[index],
index=MultiIndex.from_arrays([index,
self[columns]]))
return indexed.unstack(columns)

Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9478,6 +9478,47 @@ def test_pivot_integer_bug(self):
repr(result)
self.assert_numpy_array_equal(result.columns, ['A', 'B'])

def test_pivot_index_none(self):
# gh-3962
data = {
'index': ['A', 'B', 'C', 'C', 'B', 'A'],
'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
'values': [1., 2., 3., 3., 2., 1.]
}

frame = DataFrame(data).set_index('index')
result = frame.pivot(columns='columns', values='values')
expected = DataFrame({
'One': {'A': 1., 'B': 2., 'C': 3.},
'Two': {'A': 1., 'B': 2., 'C': 3.}
})

expected.index.name, expected.columns.name = 'index', 'columns'
assert_frame_equal(result, expected)

# omit values
result = frame.pivot(columns='columns')

expected.columns = pd.MultiIndex.from_tuples([('values', 'One'),
('values', 'Two')],
names=[None, 'columns'])
expected.index.name = 'index'
assert_frame_equal(result, expected, check_names=False)
self.assertEqual(result.index.name, 'index',)
self.assertEqual(result.columns.names, (None, 'columns'))
expected.columns = expected.columns.droplevel(0)

data = {
'index': range(7),
'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
'values': [1., 2., 3., 3., 2., 1.]
}

result = frame.pivot(columns='columns', values='values')

expected.columns.name = 'columns'
assert_frame_equal(result, expected)

def test_reindex(self):
newFrame = self.frame.reindex(self.ts1.index)

Expand Down