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

Feature Request: Provide dict object for pd.Series.to_dict() #16122

Closed
jolespin opened this Issue Apr 25, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@jolespin

jolespin commented Apr 25, 2017

I frequently use the .to_dict() method and was wondering how difficult it would be to implement a dict_obj argument to specify what type of dictionary will be used. For example, if one was interested in preserving the order:

from collections import OrderedDict

pd.Series(list("abcd")).to_dict(dict_obj=OrderedDict)
@TomAugspurger

This comment has been minimized.

Show comment
Hide comment
@TomAugspurger

TomAugspurger Apr 25, 2017

Contributor

The implementation of to_dict is just

    def to_dict(self):
        """
        Convert Series to {label -> value} dict

        Returns
        -------
        value_dict : dict
        """
        return dict(compat.iteritems(self))

So, if you wanted to take an argument (type, kind?) with a default of dict I think that'd be fine.

Contributor

TomAugspurger commented Apr 25, 2017

The implementation of to_dict is just

    def to_dict(self):
        """
        Convert Series to {label -> value} dict

        Returns
        -------
        value_dict : dict
        """
        return dict(compat.iteritems(self))

So, if you wanted to take an argument (type, kind?) with a default of dict I think that'd be fine.

@jolespin

This comment has been minimized.

Show comment
Hide comment
@jolespin

jolespin Apr 25, 2017

Thanks! This will come in handy b/c a lot of my scripts I convert between dicts and series. I'm not sure what keyword to use but I think type and kind sound great. Others I thought of are class, container, object, etc.

jolespin commented Apr 25, 2017

Thanks! This will come in handy b/c a lot of my scripts I convert between dicts and series. I'm not sure what keyword to use but I think type and kind sound great. Others I thought of are class, container, object, etc.

@TomAugspurger

This comment has been minimized.

Show comment
Hide comment
@TomAugspurger

TomAugspurger Apr 25, 2017

Contributor

class wouldn't work since that's a keyword. If you have a chance, you could skim through parts of the standard library that have something similar. Not much comes to my mind other than argparse, which IIRC uses a type keyword argument.

Contributor

TomAugspurger commented Apr 25, 2017

class wouldn't work since that's a keyword. If you have a chance, you could skim through parts of the standard library that have something similar. Not much comes to my mind other than argparse, which IIRC uses a type keyword argument.

@jolespin

This comment has been minimized.

Show comment
Hide comment
@jolespin

jolespin Apr 25, 2017

Oh yea that's true, type does get temporarily repurposed in argparse. The only other thing that comes to mind is in matplotlib with format like fig.savefig(path, format="pdf")

jolespin commented Apr 25, 2017

Oh yea that's true, type does get temporarily repurposed in argparse. The only other thing that comes to mind is in matplotlib with format like fig.savefig(path, format="pdf")

@dwkenefick

This comment has been minimized.

Show comment
Hide comment
@dwkenefick

dwkenefick Apr 30, 2017

Contributor

I can take a crack at this one if we do want to implement it. Quick questions:

  1. Should we only allow certain types, e.g. OrderedDict and a few other easy ones from collections, like deque? Some of the others might be a little odd, like defaultdict.
  2. Do we want to implement this for pd.DataFrame too?

I'll use type as the keyword - I couldn't find any strong precedent in the standard library. kind is used elsewhere in pandas, for example DataFrame.plot, but that feels different.

@jolespin - You probably already know this but here is an easy way to do what you want:

import pandas as pd
from collections import OrderedDict

series = pd.Series({'a': 1, 'b': 2})
OrderedDict(series)
Contributor

dwkenefick commented Apr 30, 2017

I can take a crack at this one if we do want to implement it. Quick questions:

  1. Should we only allow certain types, e.g. OrderedDict and a few other easy ones from collections, like deque? Some of the others might be a little odd, like defaultdict.
  2. Do we want to implement this for pd.DataFrame too?

I'll use type as the keyword - I couldn't find any strong precedent in the standard library. kind is used elsewhere in pandas, for example DataFrame.plot, but that feels different.

@jolespin - You probably already know this but here is an easy way to do what you want:

import pandas as pd
from collections import OrderedDict

series = pd.Series({'a': 1, 'b': 2})
OrderedDict(series)
@TomAugspurger

This comment has been minimized.

Show comment
Hide comment
@TomAugspurger

TomAugspurger May 1, 2017

Contributor

into might be a decent name.

If we wanted to do checking, then we could check issubclass(into, collections.Mapping), which is the ABC for dict-like objects.

It would be nice to do this for DataFrame as well.

Contributor

TomAugspurger commented May 1, 2017

into might be a decent name.

If we wanted to do checking, then we could check issubclass(into, collections.Mapping), which is the ABC for dict-like objects.

It would be nice to do this for DataFrame as well.

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 3, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 3, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 3, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 5, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 5, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 5, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 6, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 6, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 8, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 11, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 12, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 12, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 12, 2017

dwkenefick added a commit to dwkenefick/pandas that referenced this issue May 16, 2017

TomAugspurger added a commit that referenced this issue May 16, 2017

ENH: Provide dict object for to_dict() #16122 (#16220)
* ENH: Provide dict object for to_dict() #16122

* ENH: Provide dict object for to_dict() #16122

* ENH: Provide dict object for to_dict() #16122

* ENH: Provide dict object for to_dict() #16122

* ENH: Provide dict object for to_dict() #16122

* ENH: Provide dict object for to_dict() #16122

* ENH: Provide dict object for to_dict() #16122

pawroman added a commit to pawroman/pandas that referenced this issue May 18, 2017

Merge remote-tracking branch 'upstream/master' into fix-panel-fillna
* upstream/master: (48 commits)
  BUG: Categorical comparison with unordered (pandas-dev#16339)
  ENH: Adding 'protocol' parameter to 'to_pickle'.
  PERF: improve MultiIndex get_loc performance (pandas-dev#16346)
  TST: remove pandas-datareader xfail as 0.4.0 works (pandas-dev#16374)
  TST: followup to pandas-dev#16364, catch errstate warnings (pandas-dev#16373)
  DOC: new oauth token
  TST: Add test for clip-na (pandas-dev#16369)
  ENH: Draft metadata specification doc for Apache Parquet (pandas-dev#16315)
  MAINT: Add .iml to .gitignore (pandas-dev#16368)
  BUG/API: Categorical constructor scalar categories (pandas-dev#16340)
  ENH: Provide dict object for to_dict() pandas-dev#16122 (pandas-dev#16220)
  PERF: improved clip performance (pandas-dev#16364)
  DOC: try new token for docs
  DOC: try with new secure token
  DOC: add developer section to the docs
  DEPS: Drop Python 3.4 support (pandas-dev#16303)
  DOC: remove credential helper
  DOC: force fetch on build docs
  DOC: redo dev docs access token
  DOC: add dataframe construction in merge_asof example (pandas-dev#16348)
  ...

pcluo added a commit to pcluo/pandas that referenced this issue May 22, 2017

ENH: Provide dict object for to_dict() pandas-dev#16122 (pandas-dev#1…
…6220)

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

stangirala added a commit to stangirala/pandas that referenced this issue Jun 11, 2017

ENH: Provide dict object for to_dict() pandas-dev#16122 (pandas-dev#1…
…6220)

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122

* ENH: Provide dict object for to_dict() pandas-dev#16122
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment