question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dwkenefickcommented, 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)
1reaction
TomAugspurgercommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.Series.to_dict — pandas 1.5.2 documentation
Mapping subclass to use as the return object. Can be the actual class or an empty instance of the mapping type you want....
Read more >
Python | Pandas Series.to_dict() - GeeksforGeeks
Pandas Series.to_dict() function is used to convert the given Series object to {label -> value} dict or dict-like object.
Read more >
python - AttributeError: 'dict' object has no attribute 'predictors'
The dict.items iterates over the key-value pairs of a dictionary. Therefore for key, value in dictionary.items() will loop over each pair.
Read more >
Convert Pandas Series to Dict in Python - FavTutor
Series can store all types of data such as strings, integer, float, and other python objects. Each element in this data structure has...
Read more >
arcgis.features module | ArcGIS API for Python
Creates a Feature object from a dictionary. Returns ... The itemID field from an upload() response, corresponding with the appendUploadId REST API argument....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found