Skip to content
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

Deprecate Aliases as orient Argument in DataFrame.to_dict #32516

Merged
merged 32 commits into from Mar 14, 2020

Conversation

elmonsomiat
Copy link
Contributor

@elmonsomiat elmonsomiat commented Mar 7, 2020

@@ -352,6 +352,7 @@ Other
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
- Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`)
- Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`)
- Using an `orient` argument not listed inside :meth:`DataFrame.to_dict` will raise a ``ValueError`` (:issue:`32515`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MomIsBestFriend I cannot see why the doc CI are failing with this added line. Not sure if this what's new info had to be added in some other place or I am not supposed to add this myself 🤔 Any ideas?
(btw, just checked and it works fine on my local machine)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elmonsomiat I am not sure why the docs are failing, maybe try to merge master?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like there are too many lines in this section (I tried adding my comment on another section within the same file and it works). Adding this new line breaks it. Not sure if it's OK to leave it as it is, without chaning the whatsnew file!

@WillAyd
Copy link
Member

WillAyd commented Mar 7, 2020

On board with the change but I think should wait until 2.0 since this could break things (though yes undocumented)

@WillAyd
Copy link
Member

WillAyd commented Mar 7, 2020

Should deprecate for 1.1

Copy link
Member

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. ex @WillAyd comment that we should deprecate it, I believe that we should at least give a Deprecation warning. so I was thinking some thing with the style of:

orient = orient.lower()

if orient in {'i', 's', 'sp', ... }:
    warnings.warn("Using short name for 'orient' is deprecated...", DeprecationWarning, stacklevel=2)
    if orient == "i":
        orient = "index"
    elif orient == "l":
         orient = "list"
     ...

And then below the rest of your code.

@ShaharNaveh
Copy link
Member

I see that in c598b32 you reverted some code, pytest handles DeprecationWarning a bit different than the usual

with pytest.raises(Exception):
    foo()

You can read more about how pytest deals with DeprecationWarning at https://docs.pytest.org/en/latest/warnings.html#ensuring-code-triggers-a-deprecation-warning


To sum it up, you catch DeprecationWarning by:

with pytest.deprecated_call():
    foo()

Copy link
Member

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the comments in #32516 (comment), this looks very good (IMO)

@elmonsomiat
Copy link
Contributor Author

@MomIsBestFriend Thanks for the patient review! 👏

Copy link
Member

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only comment I have, otherwise this is LGTM.

@@ -64,14 +64,24 @@ def test_to_dict_index_not_unique_with_index_orient(self):
with pytest.raises(ValueError, match=msg):
df.to_dict(orient="index")

def test_to_dict_invalid_orient(self):
@pytest.mark.parametrize("orient", ["xinvalid"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that parameteration is redundant, can you remove it and have this test as it was before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, agreed

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice! lgtm

Copy link
Member

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -174,7 +174,7 @@ Deprecations
- Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`)
- :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`)
- Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`)
-
- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions (:issue:`32515`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has deprecated accepting short names

pandas/core/frame.py Outdated Show resolved Hide resolved
pandas/core/frame.py Outdated Show resolved Hide resolved
pandas/core/frame.py Show resolved Hide resolved
pandas/core/frame.py Show resolved Hide resolved
@jreback jreback added this to the 1.1 milestone Mar 14, 2020
@jreback
Copy link
Contributor

jreback commented Mar 14, 2020

/azp run

@azure-pipelines
Copy link
Contributor

Azure Pipelines successfully started running 1 pipeline(s).

@WillAyd WillAyd merged commit e734449 into pandas-dev:master Mar 14, 2020
@WillAyd
Copy link
Member

WillAyd commented Mar 14, 2020

Thanks @elmonsomiat - very impressive first contribution

SeeminSyed pushed a commit to CSCD01-team01/pandas that referenced this pull request Mar 22, 2020
@shubh2u
Copy link

shubh2u commented May 31, 2020

Just reading through some PRs to understand the contribution process better. I see this sentence in the documentation "Abbreviations are allowed. s indicates series and sp indicates split." Is a corresponding doc change needed for this?

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html

@@ -1401,11 +1401,45 @@ def to_dict(self, orient="dict", into=dict):
)
# GH16122
into_c = com.standardize_mapping(into)
if orient.lower().startswith("d"):

orient = orient.lower()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 1328 above might also need a change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think so - are you interested in submitting a PR to update the documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be removed in a future PR. Check below, there is a FutureWarning that says this will not be accepted anymore, then we can remove this line.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, so to confirm, in the future any code like
df.to_dict(orient='s') will stop working?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, it should

@elmonsomiat
Copy link
Contributor Author

Oh, yes, this sentence should be removed. This is not the case for to_json and I think they should be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DataFrame DataFrame data structure Deprecate Functionality to remove in pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

df.to_dict() accepts orient which are not in the list of options
7 participants