Skip to content

Using str() in .map() on floats gives string with higher precision than before #13228

Closed
@marcomayer

Description

@marcomayer

Code Sample, a copy-pastable example if possible


# In 0.17.1:
x = pd.Series(1/3)
x.map(lambda x: str(x)).to_dict()
{0: '0.333333333333'}

x.astype(str).map(lambda x: x).to_dict()
{0: '0.333333333333'}

# In 0.18.1:
x = pd.Series(1/3)
x.map(lambda x: str(x)).to_dict()
{0: '0.3333333333333333'}

x.astype(str).map(lambda x: x).to_dict()
{0: '0.333333333333'}

Expected Output

I'd expect the same output as in 0.17.x and before.

I do this a lot to convert floats to decimal.decimal with .map(lambda x: D(str(x))) which is slightly faster than using .astype(str).map(D).

This also messed up many of my unit-tests where I convert DFs to string dicts. Thanks to those I found this at all.

I checked the change docs but couldn't find something that points to why this should have changed.

output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions