Skip to content

API/CLN: Have toplevel pd.pivot mirror pivot instead of pivot_simple #22209

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 12 commits into from
Aug 8, 2018
Prev Previous commit
Next Next commit
remove _slow_pivot, _pivot_simple and associated test
  • Loading branch information
Matt Roeschke committed Aug 7, 2018
commit fae6c5b87895d0ad04e20733e5316fa779a3dcf6
66 changes: 0 additions & 66 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from pandas.core.reshape.concat import concat
from pandas.core.series import Series
from pandas.core.frame import DataFrame
from pandas.core.groupby import Grouper
from pandas.core.reshape.util import cartesian_product
from pandas.core.index import Index, MultiIndex, _get_objs_combined_axis
Expand Down Expand Up @@ -623,68 +622,3 @@ def _get_names(arrs, names, prefix='row'):
names = list(names)

return names


def _pivot_simple(index, columns, values):
"""
Produce 'pivot' table based on 3 columns of this DataFrame.
Uses unique values from index / columns and fills with values.
Parameters
----------
index : ndarray
Labels to use to make new frame's index
columns : ndarray
Labels to use to make new frame's columns
values : ndarray
Values to use for populating new frame's values
Notes
-----
Obviously, all 3 of the input arguments must have the same length
This is ONLY used for testing in pandas/test/test_panel.py
Returns
-------
DataFrame
See also
--------
DataFrame.pivot_table : generalization of pivot that can handle
duplicate values for one index/column pair
"""
if (len(index) != len(columns)) or (len(columns) != len(values)):
raise AssertionError('Length of index, columns, and values must be the'
' same')
if len(index) == 0:
return DataFrame(index=[])
hindex = MultiIndex.from_arrays([index, columns])
series = Series(values.ravel(), index=hindex)
series = series.sort_index(level=0)
return series.unstack()


def _slow_pivot(index, columns, values):
"""
Produce 'pivot' table based on 3 columns of this DataFrame.
Uses unique values from index / columns and fills with values.

Parameters
----------
index : string or object
Column name to use to make new frame's index
columns : string or object
Column name to use to make new frame's columns
values : string or object
Column name to use for populating new frame's values

Could benefit from some Cython here.

Note
----
This is ONLY used for testing in pandas/test/test_panel.py
"""
tree = {}
for i, (idx, col) in enumerate(zip(index, columns)):
if col not in tree:
tree[col] = {}
branch = tree[col]
branch[idx] = values[i]

return DataFrame(tree)
24 changes: 0 additions & 24 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2676,30 +2676,6 @@ def test_join(self):
pytest.raises(Exception, lp1.join,
self.panel.filter(['ItemB', 'ItemC']))

def test_pivot(self):
with catch_warnings(record=True):
from pandas.core.reshape.pivot import _slow_pivot, _pivot_simple

one, two, three = (np.array([1, 2, 3, 4, 5]),
np.array(['a', 'b', 'c', 'd', 'e']),
np.array([1, 2, 3, 5, 4.]))
df = _pivot_simple(one, two, three)
assert df['a'][1] == 1
assert df['b'][2] == 2
assert df['c'][3] == 3
assert df['d'][4] == 5
assert df['e'][5] == 4
assert_frame_equal(df, _slow_pivot(one, two, three))

# weird overlap, TODO: test?
a, b, c = (np.array([1, 2, 3, 4, 4]),
np.array(['a', 'a', 'a', 'a', 'a']),
np.array([1., 2., 3., 4., 5.]))
pytest.raises(Exception, _pivot_simple, a, b, c)

# corner case, empty
df = _pivot_simple(np.array([]), np.array([]), np.array([]))


def test_panel_index():
index = panelm.panel_index([1, 2, 3, 4], [1, 2, 3])
Expand Down