Repository navigation
keep index of series when recoding a series - #5548
Conversation
|
Change looks simple, i.e. it looks correct even though I don't know the details of the function. |
|
@josef-pkt I can add a unit test for Here is an example showing the issue: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.factorplots import interaction_plot, _recode
# create DataFrame with non-contiguous indices
data = pd.DataFrame({
'factor_a': (['a'] * 10) + (['b'] * 10),
'factor_b': ['c', 'd'] * 10,
'dependent': np.random.rand(20)},
index=np.arange(0, 40, 2, dtype=int))
## 'visual' test
# wrong interaction plot (because new indices mismatch)
interaction_plot(data['factor_a'], data['factor_b'], data['dependent'])
plt.show()
# correct interaction plot (external fix, index matches new index of _recode)
data = data.reset_index(drop=True)
interaction_plot(data['factor_a'], data['factor_b'], data['dependent'])
plt.show()
## unit test for _recode
data.index = np.arange(0, 40, 2, dtype=int)
factor_a = _recode(data['factor_a'], {'a': 0, 'b': 1})
assert (factor_a.index == data.index).all() |
Codecov Report
@@ Coverage Diff @@
## master #5548 +/- ##
==========================================
+ Coverage 82.01% 82.02% +0.01%
==========================================
Files 588 588
Lines 93026 93031 +5
Branches 10313 10313
==========================================
+ Hits 76298 76312 +14
+ Misses 14363 14357 -6
+ Partials 2365 2362 -3
Continue to review full report at Codecov.
|
|
I added a test case. I'm not familiar with pytest. Please let me know whether I need to adjust the test case. |
|
@josef-pkt I think the integration tests failed for some unrelated convergence issue. |
|
LGMT. Thanks. |
interaction_plottries to recode a 'list' of string to 'list' of integers:statsmodels/statsmodels/graphics/factorplots.py
Line 106 in 0c3f6ce
This causes problems when the input is a pandas Series which has indices which are not
range(length)(in my case the series had not contiguous indices). After recording,interaction_plottries to combine the two independent variables and dependent variable with pandas:statsmodels/statsmodels/graphics/factorplots.py
Line 108 in 0c3f6ce
Pandas notices it should create a DataFrame from several Series which have (through the recoding) potentially different indices, so it adds NAN for the non-overlapping values. This leads to a wrong interaction plot.
I fixed this behavior in
_recodeby keeping the original index of the series.