Skip to main content

What is it used for?

Convert a Pandas DataFrame/Series with dtype str/string/object to the best available dtypes

Installation

pip install a-pandas-ex-string-to-dtypes

Usage

    from a_pandas_ex_string_to_dtypes import pd_add_string_to_dtypes

    import pandas as pd

    pd_add_string_to_dtypes()

    df = pd.read_csv("https://github.com/pandas-dev/pandas/raw/main/doc/data/titanic.csv")

    print(df)

    print(df.dtypes)   

    

    

         PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked

    0              1         0       3  ...   7.2500   NaN         S

    1              2         1       1  ...  71.2833   C85         C

    2              3         1       3  ...   7.9250   NaN         S

    3              4         1       1  ...  53.1000  C123         S

    4              5         0       3  ...   8.0500   NaN         S

    ..           ...       ...     ...  ...      ...   ...       ...

    886          887         0       2  ...  13.0000   NaN         S

    887          888         1       1  ...  30.0000   B42         S

    888          889         0       3  ...  23.4500   NaN         S

    889          890         1       1  ...  30.0000  C148         C

    890          891         0       3  ...   7.7500   NaN         Q

    [891 rows x 12 columns]  

    

    PassengerId      int64

    Survived         int64

    Pclass           int64

    Name            object

    Sex             object

    Age            float64

    SibSp            int64

    Parch            int64

    Ticket          object

    Fare           float64

    Cabin           object

    Embarked        object

    dtype: object     

    

    

    

    

    

    dfstring = pd.concat(

        [df[x].astype("string") for x in df.columns], axis=1, ignore_index=True

    )

    dfstring.columns=df.columns

    print(dfstring)

    print(dfstring.dtypes)  

    

        PassengerId Survived Pclass  ...     Fare Cabin Embarked

    0             1        0      3  ...     7.25  <NA>        S

    1             2        1      1  ...  71.2833   C85        C

    2             3        1      3  ...    7.925  <NA>        S

    3             4        1      1  ...     53.1  C123        S

    4             5        0      3  ...     8.05  <NA>        S

    ..          ...      ...    ...  ...      ...   ...      ...

    886         887        0      2  ...     13.0  <NA>        S

    887         888        1      1  ...     30.0   B42        S

    888         889        0      3  ...    23.45  <NA>        S

    889         890        1      1  ...     30.0  C148        C

    890         891        0      3  ...     7.75  <NA>        Q

    [891 rows x 12 columns]    

    

    

    PassengerId    string

    Survived       string

    Pclass         string

    Name           string

    Sex            string

    Age            string

    SibSp          string

    Parch          string

    Ticket         string

    Fare           string

    Cabin          string

    Embarked       string

    dtype: object    

    

    

    

    converted = dfstring.ds_string_to_best_dtype()

    print(converted)

    print(converted.dtypes)

         PassengerId  Survived  Pclass  ...     Fare Cabin Embarked

    0              1         0       3  ...   7.2500  <NA>        S

    1              2         1       1  ...  71.2833   C85        C

    2              3         1       3  ...   7.9250  <NA>        S

    3              4         1       1  ...  53.1000  C123        S

    4              5         0       3  ...   8.0500  <NA>        S

    ..           ...       ...     ...  ...      ...   ...      ...

    886          887         0       2  ...  13.0000  <NA>        S

    887          888         1       1  ...  30.0000   B42        S

    888          889         0       3  ...  23.4500  <NA>        S

    889          890         1       1  ...  30.0000  C148        C

    890          891         0       3  ...   7.7500  <NA>        Q

    [891 rows x 12 columns]    

    

    

    PassengerId      uint16

    Survived          uint8

    Pclass            uint8

    Name             string

    Sex            category

    Age              object

    SibSp             uint8

    Parch             uint8

    Ticket           object

    Fare            float64

    Cabin          category

    Embarked       category

    dtype: object    

    

    

        Parameters:

            df: Union[pd.DataFrame, pd.Series]

        Returns:

            Union[pd.DataFrame, pd.Series]

Metadata

Release files for a-pandas-ex-string-to-dtypes 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for a-pandas-ex-string-to-dtypes 0.1
File Size Uploaded Provenance Actions
a_pandas_ex_string_to_dtypes-0.1.tar.gz 6.3 kB Without provenance Details for a_pandas_ex_string_to_dtypes-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for a-pandas-ex-string-to-dtypes 0.1
File Interpreter ABI Platform Actions
a_pandas_ex_string_to_dtypes-0.1-py3-none-any.whl Python 3 none any Details for a_pandas_ex_string_to_dtypes-0.1-py3-none-any.whl

Total release size: 14.9 kB

Release files / a_pandas_ex_string_to_dtypes-0.1.tar.gz

Download URL a_pandas_ex_string_to_dtypes-0.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
850db3010565b38d5d3eb8fc0f3a97c8223b7d8b595835071de1934c39d0bf91
BLAKE2b-256 checksum
How to use checksums
5acd0ee0f6357f7fe2d7f9822eef64ffc9dd047cb288615e0d3ccec1eb39a9be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / a_pandas_ex_string_to_dtypes-0.1-py3-none-any.whl

Download URL a_pandas_ex_string_to_dtypes-0.1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f428c8564bc589b102de2a8bae982a4c659b899f1214ae506c86207ff5c8769e
BLAKE2b-256 checksum
How to use checksums
0df2934ee563eee188b6d7a7d267cc85268364e31c2405ed6aa04622f194c941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for a-pandas-ex-string-to-dtypes 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page