-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
melt moved into its own module #18148
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
Conversation
pandas/core/reshape/melt.py
Outdated
from pandas.core.dtypes.missing import notna | ||
import pandas.core.dtypes.concat as _concat | ||
|
||
from pandas.core.series import Series |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just import from pandas as much as you can rather than the implementation (e.g. from pandas import DataFrame)
other='DataFrame.melt')) | ||
def melt(frame, id_vars=None, value_vars=None, var_name=None, | ||
value_name='value', col_level=None): | ||
# TODO: what about the existing index? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is all cut/paste?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I just cut and pasted everything. I thought that's what you wanted. Let me know if you wanted something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes just confirming
pandas/core/reshape/reshape.py
Outdated
@@ -33,6 +33,7 @@ | |||
from pandas.core.frame import _shared_docs | |||
from pandas.util._decorators import Appender | |||
from pandas.core.index import Index, MultiIndex, _get_na_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect linting will fail here, you need to remove imports that are no longer needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I didn't modify anything. I wanted to make sure we were on the right track first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh ok (cleanup imports here is fine though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cleaned up imports
pandas/core/reshape/melt.py
Outdated
from pandas.core.dtypes.common import is_list_like | ||
from pandas import compat | ||
|
||
from pandas.core.frame import DataFrame |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import these from pandas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reshape.py uses the full path. Also, I'm getting an import error when I do from pandas import DataFrame
pandas/core/reshape/reshape.py
Outdated
from pandas.core.index import Index, MultiIndex, _get_na_value | ||
from pandas.core.reshape.melt import melt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this import should not be necessary, instead directly import from the new module
rebase i would also move lreshape and wide_to_long to melt.py |
@@ -24,8 +24,8 @@ | |||
from pandas.core.panel import Panel, WidePanel | |||
from pandas.core.panel4d import Panel4D | |||
from pandas.core.reshape.reshape import ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see if you can move these reshape imports here to pandas.core.reshape.api (with the other ones); reshape.api is already imported to pandas.__init__
, just make more sense there
from pandas.core.categorical import Categorical | ||
|
||
from pandas.core.frame import DataFrame | ||
from pandas.core.index import MultiIndex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can do this now, or can in a followup. These top-level imports needs to be from the pandas namespace; some can be ABC
, e.g. ABCMultiIndex
, which you can import here. The others should be imported inside the functions themselves if needed. This allows this module to be imported irrespective of its import ordering.
if id_vars is not None: | ||
if not is_list_like(id_vars): | ||
id_vars = [id_vars] | ||
elif (isinstance(frame.columns, MultiIndex) and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ABCMultiIndex
if value_vars is not None: | ||
if not is_list_like(value_vars): | ||
value_vars = [value_vars] | ||
elif (isinstance(frame.columns, MultiIndex) and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
frame.columns = frame.columns.get_level_values(col_level) | ||
|
||
if var_name is None: | ||
if isinstance(frame.columns, MultiIndex): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
mdata[col] = np.asanyarray(frame.columns | ||
._get_level_values(i)).repeat(N) | ||
|
||
return DataFrame(mdata, columns=mcolumns) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can
from pandas import DataFrame
here
mdata = {} | ||
pivot_cols = [] | ||
|
||
for target, names in zip(keys, values): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can
from pandas.core.dtypes.concat import _concat
here
if not mask.all(): | ||
mdata = dict((k, v[mask]) for k, v in compat.iteritems(mdata)) | ||
|
||
return DataFrame(mdata, columns=id_cols + pivot_cols) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from pandas import DataFrame
@@ -11,8 +11,8 @@ | |||
|
|||
from pandas.util.testing import assert_frame_equal | |||
|
|||
from pandas.core.reshape.reshape import ( | |||
melt, lreshape, get_dummies, wide_to_long) | |||
from pandas.core.reshape.reshape import get_dummies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use from pandas import get_dummies, melt, lreshape, wide_to_long
.
in tests we always want to use the user call (rather than a specific import)
Codecov Report
@@ Coverage Diff @@
## master #18148 +/- ##
==========================================
- Coverage 91.42% 91.41% -0.02%
==========================================
Files 163 164 +1
Lines 50068 50076 +8
==========================================
- Hits 45777 45776 -1
- Misses 4291 4300 +9
Continue to review full report at Codecov.
|
I would keep the code changes / clean-up for a follow-up PR, and keep it here to just moving things |
thanks @tdpetrou ok can you do a followup-PR to implement the small cleans before we move on to your proposed changes. thanks. |
This pull request is to make PR #17677 easier and was requested by @jreback here