Skip to content

Commit 0f5092c

Browse files
committed
fix names on return structure
1 parent ed7d927 commit 0f5092c

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

doc/source/computation.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,12 @@ can even be omitted:
549549
.. ipython:: python
550550
551551
covs = df[['B','C','D']].rolling(window=50).cov(df[['A','B','C']], pairwise=True)
552-
covs.iloc[-50].unstack()
552+
covs.unstack(-1).iloc[-50]
553553
554554
.. ipython:: python
555555
556556
correls = df.rolling(window=50).corr()
557-
correls.iloc[-50].unstack()
557+
correls.unstack(-1).iloc[-50]
558558
559559
You can efficiently retrieve the time series of correlations between two
560560
columns using ``.loc`` indexing:
@@ -567,7 +567,7 @@ columns using ``.loc`` indexing:
567567
.. ipython:: python
568568
569569
@savefig rolling_corr_pairwise_ex.png
570-
correls[('A', 'C')].plot()
570+
correls.unstack(-1).[('A', 'C')].plot()
571571
572572
.. _stats.aggregate:
573573

doc/source/whatsnew/v0.20.0.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,10 @@ See the section on :ref:`Windowed Binary Operations <stats.moments.binary>` for
781781
.. ipython:: python
782782

783783
np.random.seed(1234)
784-
df = DataFrame(np.random.rand(100, 2),
785-
columns=['A', 'B'],
786-
index=pd.date_range('20160101', periods=100, freq='D'))
784+
df = pd.DataFrame(np.random.rand(100, 2),
785+
columns=pd.Index(['A', 'B'], name='bar'),
786+
index=pd.date_range('20160101',
787+
periods=100, freq='D', name='foo'))
787788
df
788789

789790
Old Behavior:
@@ -805,12 +806,11 @@ New Behavior:
805806
res = df.rolling(12).corr()
806807
res
807808

808-
Retrieving a correlation matrix for a specified index
809+
Retrieving a correlation matrix for a cross-section
809810

810811
.. ipython:: python
811812

812-
res.iloc[-1].unstack()
813-
813+
df.rolling(12).corr().loc['2016-04-07']
814814

815815
.. _whatsnew_0200.api_breaking.hdfstore_where:
816816

pandas/core/window.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ def dataframe_from_int_dict(data, frame_template):
17071707

17081708
# TODO: not the most efficient (perf-wise)
17091709
# though not bad code-wise
1710-
from pandas import Panel, MultiIndex
1710+
from pandas import Panel, MultiIndex, Index
17111711
p = Panel.from_dict(results).swapaxes('items', 'major')
17121712
if len(p.major_axis) > 0:
17131713
p.major_axis = arg1.columns[p.major_axis]
@@ -1721,14 +1721,16 @@ def dataframe_from_int_dict(data, frame_template):
17211721
else:
17221722

17231723
result = DataFrame(
1724-
index=MultiIndex(levels=[arg1.columns, arg2.index],
1724+
index=MultiIndex(levels=[arg1.index, arg1.columns],
17251725
labels=[[], []]),
1726-
columns=arg1.columns,
1726+
columns=arg2.columns,
17271727
dtype='float64')
17281728

1729-
# reset our names
1730-
result.columns.name = None
1731-
result.index.names = ['major', 'minor']
1729+
# reset our names to arg1 names
1730+
# careful not to mutate the original names
1731+
result.columns = Index(result.columns).set_names(None)
1732+
result.index = result.index.set_names(
1733+
[arg1.index.name, arg1.columns.name])
17321734

17331735
return result
17341736

pandas/tests/test_window.py

+38-20
Original file line numberDiff line numberDiff line change
@@ -2660,11 +2660,13 @@ def test_rolling_functions_window_non_shrinkage(self):
26602660
def test_rolling_functions_window_non_shrinkage_binary(self):
26612661

26622662
# corr/cov return a MI DataFrame
2663-
df = DataFrame([[1, 5], [3, 2], [3, 9], [-1, 0]], columns=['A', 'B'])
2663+
df = DataFrame([[1, 5], [3, 2], [3, 9], [-1, 0]],
2664+
columns=Index(['A', 'B'], name='foo'),
2665+
index=Index(range(4), name='bar'))
26642666
df_expected = DataFrame(
2665-
columns=df.columns,
2667+
columns=Index(['A', 'B']),
26662668
index=pd.MultiIndex.from_product([df.index, df.columns],
2667-
names=['major', 'minor']),
2669+
names=['bar', 'foo']),
26682670
dtype='float64')
26692671
functions = [lambda x: (x.rolling(window=10, min_periods=5)
26702672
.cov(x, pairwise=True)),
@@ -2739,17 +2741,17 @@ def test_moment_functions_zero_length_pairwise(self):
27392741

27402742
df1 = DataFrame()
27412743
df1_expected = df1
2742-
df2 = DataFrame(columns=['a'])
2744+
df2 = DataFrame(columns=Index(['a'], name='foo'),
2745+
index=Index([], name='bar'))
27432746
df2['a'] = df2['a'].astype('float64')
27442747

27452748
df1_expected = DataFrame(
2746-
index=pd.MultiIndex.from_product([df1.columns, df1.index],
2747-
names=['major', 'minor']),
2748-
columns=df1.columns)
2749+
index=pd.MultiIndex.from_product([df1.index, df1.columns]),
2750+
columns=Index([]))
27492751
df2_expected = DataFrame(
2750-
index=pd.MultiIndex.from_product([df2.columns, df2.index],
2751-
names=['major', 'minor']),
2752-
columns=df2.columns,
2752+
index=pd.MultiIndex.from_product([df2.index, df2.columns],
2753+
names=['bar', 'foo']),
2754+
columns=Index(['a']),
27532755
dtype='float64')
27542756

27552757
functions = [lambda x: (x.expanding(min_periods=5)
@@ -2770,35 +2772,51 @@ def test_moment_functions_zero_length_pairwise(self):
27702772

27712773
def test_expanding_cov_pairwise_diff_length(self):
27722774
# GH 7512
2773-
df1 = DataFrame([[1, 5], [3, 2], [3, 9]], columns=['A', 'B'])
2774-
df1a = DataFrame([[1, 5], [3, 9]], index=[0, 2], columns=['A', 'B'])
2775-
df2 = DataFrame([[5, 6], [None, None], [2, 1]], columns=['X', 'Y'])
2776-
df2a = DataFrame([[5, 6], [2, 1]], index=[0, 2], columns=['X', 'Y'])
2775+
df1 = DataFrame([[1, 5], [3, 2], [3, 9]],
2776+
columns=Index(['A', 'B'], name='foo'))
2777+
df1a = DataFrame([[1, 5], [3, 9]],
2778+
index=[0, 2],
2779+
columns=Index(['A', 'B'], name='foo'))
2780+
df2 = DataFrame([[5, 6], [None, None], [2, 1]],
2781+
columns=Index(['X', 'Y'], name='foo'))
2782+
df2a = DataFrame([[5, 6], [2, 1]],
2783+
index=[0, 2],
2784+
columns=Index(['X', 'Y'], name='foo'))
2785+
# TODO: xref gh-15826
2786+
# .loc is not preserving the names
27772787
result1 = df1.expanding().cov(df2a, pairwise=True).loc[2]
27782788
result2 = df1.expanding().cov(df2a, pairwise=True).loc[2]
27792789
result3 = df1a.expanding().cov(df2, pairwise=True).loc[2]
27802790
result4 = df1a.expanding().cov(df2a, pairwise=True).loc[2]
27812791
expected = DataFrame([[-3.0, -6.0], [-5.0, -10.0]],
27822792
columns=['A', 'B'],
2783-
index=Index(['X', 'Y'], name='minor'))
2793+
index=Index(['X', 'Y'], name='foo'))
27842794
tm.assert_frame_equal(result1, expected)
27852795
tm.assert_frame_equal(result2, expected)
27862796
tm.assert_frame_equal(result3, expected)
27872797
tm.assert_frame_equal(result4, expected)
27882798

27892799
def test_expanding_corr_pairwise_diff_length(self):
27902800
# GH 7512
2791-
df1 = DataFrame([[1, 2], [3, 2], [3, 4]], columns=['A', 'B'])
2792-
df1a = DataFrame([[1, 2], [3, 4]], index=[0, 2], columns=['A', 'B'])
2793-
df2 = DataFrame([[5, 6], [None, None], [2, 1]], columns=['X', 'Y'])
2794-
df2a = DataFrame([[5, 6], [2, 1]], index=[0, 2], columns=['X', 'Y'])
2801+
df1 = DataFrame([[1, 2], [3, 2], [3, 4]],
2802+
columns=['A', 'B'],
2803+
index=Index(range(3), name='bar'))
2804+
df1a = DataFrame([[1, 2], [3, 4]],
2805+
index=Index([0, 2], name='bar'),
2806+
columns=['A', 'B'])
2807+
df2 = DataFrame([[5, 6], [None, None], [2, 1]],
2808+
columns=['X', 'Y'],
2809+
index=Index(range(3), name='bar'))
2810+
df2a = DataFrame([[5, 6], [2, 1]],
2811+
index=Index([0, 2], name='bar'),
2812+
columns=['X', 'Y'])
27952813
result1 = df1.expanding().corr(df2, pairwise=True).loc[2]
27962814
result2 = df1.expanding().corr(df2a, pairwise=True).loc[2]
27972815
result3 = df1a.expanding().corr(df2, pairwise=True).loc[2]
27982816
result4 = df1a.expanding().corr(df2a, pairwise=True).loc[2]
27992817
expected = DataFrame([[-1.0, -1.0], [-1.0, -1.0]],
28002818
columns=['A', 'B'],
2801-
index=Index(['X', 'Y'], name='minor'))
2819+
index=Index(['X', 'Y']))
28022820
tm.assert_frame_equal(result1, expected)
28032821
tm.assert_frame_equal(result2, expected)
28042822
tm.assert_frame_equal(result3, expected)

0 commit comments

Comments
 (0)