Skip to content

Commit

Permalink
fix issue 24180
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdong1991 committed Dec 9, 2018
1 parent 3dbb137 commit 12a447e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
25 changes: 14 additions & 11 deletions doc/source/reshaping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Reshaping by pivoting DataFrame objects
tm.N = 3
def unpivot(frame):
N, K = frame.shape
data = {'value': frame.to_numpy().ravel('F'),
'variable': np.asarray(frame.columns).repeat(N),
'date': np.tile(np.asarray(frame.index), K)}
columns = ['date', 'variable', 'value']
return pd.DataFrame(data, columns=columns)
N, K = frame.shape
data = {'value': frame.to_numpy().ravel('F'),
'variable': np.asarray(frame.columns).repeat(N),
'date': np.tile(np.asarray(frame.index), K)}
columns = ['date', 'variable', 'value']
return pd.DataFrame(data, columns=columns)
df = unpivot(tm.makeTimeDataFrame())
Expand Down Expand Up @@ -371,8 +371,8 @@ Consider a data set like this:
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
'D': np.random.randn(24),
'E': np.random.randn(24),
'F': [datetime.datetime(2013, i, 1) for i in range(1, 13)] +
[datetime.datetime(2013, i, 15) for i in range(1, 13)]})
'F': [datetime.datetime(2013, i, 1) for i in range(1, 13)]
+ [datetime.datetime(2013, i, 15) for i in range(1, 13)]})
df
We can produce pivot tables from this data very easily:
Expand All @@ -397,7 +397,8 @@ Also, you can use ``Grouper`` for ``index`` and ``columns`` keywords. For detail

.. ipython:: python
pd.pivot_table(df, values='D', index=pd.Grouper(freq='M', key='F'), columns='C')
pd.pivot_table(df, values='D', index=pd.Grouper(freq='M', key='F'),
columns='C')
You can render a nice output of the table omitting the missing values by
calling ``to_string`` if you wish:
Expand Down Expand Up @@ -693,6 +694,7 @@ handling of NaN:

.. code-block:: ipython
In [1]: x = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
In [2]: pd.factorize(x, sort=True)
Out[2]:
(array([ 2, 2, -1, 3, 0, 1]),
Expand Down Expand Up @@ -721,7 +723,8 @@ DataFrame will be pivoted in the answers below.
n = 20
cols = np.array(['key', 'row', 'item', 'col'])
df = cols + pd.DataFrame((np.random.randint(5, size=(n, 4)) // [2, 1, 2, 1]).astype(str))
df = cols + pd.DataFrame((np.random.randint(5, size=(n, 4))
// [2, 1, 2, 1]).astype(str))
df.columns = cols
df = df.join(pd.DataFrame(np.random.rand(n, 2).round(2)).add_prefix('val'))
Expand All @@ -734,7 +737,7 @@ Suppose we wanted to pivot ``df`` such that the ``col`` values are columns,
``row`` values are the index, and the mean of ``val0`` are the values? In
particular, the resulting DataFrame should look like:

.. code-block:: ipython
.. note::

col col0 col1 col2 col3 col4
row
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ exclude =
doc/source/comparison_with_sas.rst
doc/source/comparison_with_sql.rst
doc/source/comparison_with_stata.rst
doc/source/reshaping.rst
doc/source/visualization.rst


Expand Down

0 comments on commit 12a447e

Please sign in to comment.