a-pandas-ex-fast-string


Namea-pandas-ex-fast-string JSON
Version 0.11 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/a_pandas_ex_fast_string
SummaryUp to 4x faster than Series.str.contains / Series.eq - can handle Unicode!
upload_time2022-12-20 04:55:23
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords pandas dataframe string series fast unicode
VCS
bugtrack_url
requirements numexpr pandas ujson
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Up to 4x faster than Series.str.contains / Series.eq - can handle Unicode!



```python

pip install a-pandas-ex-fast-string

```



```python

from a_pandas_ex_fast_string import pd_add_fast_string

import pandas as pd



pd_add_fast_string()



df2 = pd.read_csv(

    "https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv",

    dtype="string",

)



# To check if it can handle unicode strings

df2.Name.iloc[0] += "ö"

df2.Name.iloc[10] += "ä"

df2.Name.iloc[20] += "ü"



# converts the whole dataframe

df900 = pd.Q_convert_to_fast_string(df2.copy())





dfone = df2.copy()

# converts one column

dfone.Cabin.ds_update_fast_string()



# Let's create some DataFrames of different sizes

df9000 = pd.Q_convert_to_fast_string(

    pd.concat([df2.copy() for _ in range(10)], ignore_index=True)

)

df90000 = pd.Q_convert_to_fast_string(

    pd.concat([df2.copy() for _ in range(100)], ignore_index=True)

)

df900000 = pd.Q_convert_to_fast_string(

    pd.concat([df2.copy() for _ in range(1000)], ignore_index=True)

)

df9000000 = pd.Q_convert_to_fast_string(

    pd.concat([df2.copy() for _ in range(10000)], ignore_index=True)

)







%timeit df900.loc[df900.Name.s_string_contains('y') | df900.Name.s_string_is('Montvila, Rev. Juozas')]

%timeit df900.loc[df900.Name.str.contains('y',regex=False) | (df900.Name == 'Montvila, Rev. Juozas')]

604 µs ± 9.09 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

997 µs ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)





%timeit df9000.loc[df9000.Name.s_string_contains('y') | df9000.Name.s_string_is('Montvila, Rev. Juozas')]

%timeit df9000.loc[df9000.Name.str.contains('y',regex=False) | (df9000.Name == 'Montvila, Rev. Juozas')]

1.15 ms ± 15.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

2.77 ms ± 11.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)





%timeit df90000.loc[df90000.Name.s_string_contains('y') | df90000.Name.s_string_is('Montvila, Rev. Juozas')]

%timeit df90000.loc[df90000.Name.str.contains('y',regex=False) | (df90000.Name == 'Montvila, Rev. Juozas')]

6.45 ms ± 77.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

20.7 ms ± 166 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)





%timeit df900000.loc[df900000.Name.s_string_contains('y') | df900000.Name.s_string_is('Montvila, Rev. Juozas')]

%timeit df900000.loc[df900000.Name.str.contains('y',regex=False) | (df900000.Name == 'Montvila, Rev. Juozas')]

60.5 ms ± 853 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

206 ms ± 840 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)





%timeit df9000000.loc[df9000000.Name.s_string_contains('y') | df9000000.Name.s_string_is('Montvila, Rev. Juozas')]

%timeit df9000000.loc[df9000000.Name.str.contains('y',regex=False) | (df9000000.Name == 'Montvila, Rev. Juozas')]

596 ms ± 11.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

2.06 s ± 2.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)





# Good news: it can handle unicode characters! 

df9000.loc[df9000.Name.s_string_contains('ö')].Name

Out[14]: 

0       Braund, Mr. Owen Harrisö

891     Braund, Mr. Owen Harrisö

1782    Braund, Mr. Owen Harrisö

2673    Braund, Mr. Owen Harrisö

3564    Braund, Mr. Owen Harrisö

4455    Braund, Mr. Owen Harrisö

5346    Braund, Mr. Owen Harrisö

6237    Braund, Mr. Owen Harrisö

7128    Braund, Mr. Owen Harrisö

8019    Braund, Mr. Owen Harrisö

Name: Name, dtype: string





# Bad news: every time you modify a Series, you have to update it: 



df9000.loc[df9000.Name.s_string_contains('ö')].Name

0       Braund, Mr. Owen Harrisö

891     Braund, Mr. Owen Harrisö

1782    Braund, Mr. Owen Harrisö

2673    Braund, Mr. Owen Harrisö

3564    Braund, Mr. Owen Harrisö





df9000.loc[df9000.Name.s_string_contains('ö'), "Name"] = df9000.loc[df9000.Name.s_string_contains('ö'), "Name"] + 'Ä' # updating 



df9000.Name

0                               Braund, Mr. Owen HarrisöÄ

1       Cumings, Mrs. John Bradley (Florence Briggs Th...

2                                  Heikkinen, Miss. Laina



df9000.loc[df9000.Name.s_string_contains('ö'), "Name"]  # Exception because ds_update_fast_string was not called



Traceback (most recent call last):

  File "C:\Users\Gamer\anaconda3\envs\dfdir\lib\site-packages\IPython\core\interactiveshell.py", line 3398, in run_code

    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-7-2b0dfaf8b41c>", line 1, in <cell line: 1>

    df9000.loc[df9000.Name.s_string_contains('ö'), "Name"]

  File "C:/Users/Gamer/anaconda3/envs/dfdir/a_pandas_string_search.py", line 133, in search_contains

    wordtosearchbin, columntosearch = _get_col_word(

  File "C:/Users/Gamer/anaconda3/envs/dfdir/a_pandas_string_search.py", line 103, in _get_col_word

    return wordtosearchbin, series._stringser.__array__()

AttributeError: 'NoneType' object has no attribute '__array__'



df9000.Name.ds_update_fast_string() # Necessary after changing a Series

# you can also update the whole DataFrame: df9000 = df9000.ds_update_fast_string()

# Be careful: df9000.Name.ds_update_fast_string() returns None (inplace) 

# df9000.ds_update_fast_string() returns a DataFrame



df9000.loc[df9000.Name.s_string_contains('ö'), "Name"]  # Now it is working!



0       Braund, Mr. Owen HarrisöÄ

891     Braund, Mr. Owen HarrisöÄ

1782    Braund, Mr. Owen HarrisöÄ

2673    Braund, Mr. Owen HarrisöÄ

```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/a_pandas_ex_fast_string",
    "name": "a-pandas-ex-fast-string",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "pandas,DataFrame,string,Series,fast,unicode",
    "author": "Johannes Fischer",
    "author_email": "<aulasparticularesdealemaosp@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/05/74/0e7a4707e95520e37bd4f1e3ba8eceadcdd2a7ce78ceb25d633641cea9ff/a_pandas_ex_fast_string-0.11.tar.gz",
    "platform": null,
    "description": "\n# Up to 4x faster than Series.str.contains / Series.eq - can handle Unicode!\n\n\n\n```python\n\npip install a-pandas-ex-fast-string\n\n```\n\n\n\n```python\n\nfrom a_pandas_ex_fast_string import pd_add_fast_string\n\nimport pandas as pd\n\n\n\npd_add_fast_string()\n\n\n\ndf2 = pd.read_csv(\n\n    \"https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv\",\n\n    dtype=\"string\",\n\n)\n\n\n\n# To check if it can handle unicode strings\n\ndf2.Name.iloc[0] += \"\u00f6\"\n\ndf2.Name.iloc[10] += \"\u00e4\"\n\ndf2.Name.iloc[20] += \"\u00fc\"\n\n\n\n# converts the whole dataframe\n\ndf900 = pd.Q_convert_to_fast_string(df2.copy())\n\n\n\n\n\ndfone = df2.copy()\n\n# converts one column\n\ndfone.Cabin.ds_update_fast_string()\n\n\n\n# Let's create some DataFrames of different sizes\n\ndf9000 = pd.Q_convert_to_fast_string(\n\n    pd.concat([df2.copy() for _ in range(10)], ignore_index=True)\n\n)\n\ndf90000 = pd.Q_convert_to_fast_string(\n\n    pd.concat([df2.copy() for _ in range(100)], ignore_index=True)\n\n)\n\ndf900000 = pd.Q_convert_to_fast_string(\n\n    pd.concat([df2.copy() for _ in range(1000)], ignore_index=True)\n\n)\n\ndf9000000 = pd.Q_convert_to_fast_string(\n\n    pd.concat([df2.copy() for _ in range(10000)], ignore_index=True)\n\n)\n\n\n\n\n\n\n\n%timeit df900.loc[df900.Name.s_string_contains('y') | df900.Name.s_string_is('Montvila, Rev. Juozas')]\n\n%timeit df900.loc[df900.Name.str.contains('y',regex=False) | (df900.Name == 'Montvila, Rev. Juozas')]\n\n604 \u00b5s \u00b1 9.09 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 1,000 loops each)\n\n997 \u00b5s \u00b1 13.2 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 1,000 loops each)\n\n\n\n\n\n%timeit df9000.loc[df9000.Name.s_string_contains('y') | df9000.Name.s_string_is('Montvila, Rev. Juozas')]\n\n%timeit df9000.loc[df9000.Name.str.contains('y',regex=False) | (df9000.Name == 'Montvila, Rev. Juozas')]\n\n1.15 ms \u00b1 15.2 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 1,000 loops each)\n\n2.77 ms \u00b1 11.2 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 100 loops each)\n\n\n\n\n\n%timeit df90000.loc[df90000.Name.s_string_contains('y') | df90000.Name.s_string_is('Montvila, Rev. Juozas')]\n\n%timeit df90000.loc[df90000.Name.str.contains('y',regex=False) | (df90000.Name == 'Montvila, Rev. Juozas')]\n\n6.45 ms \u00b1 77.4 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 100 loops each)\n\n20.7 ms \u00b1 166 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\n\n\n\n\n\n%timeit df900000.loc[df900000.Name.s_string_contains('y') | df900000.Name.s_string_is('Montvila, Rev. Juozas')]\n\n%timeit df900000.loc[df900000.Name.str.contains('y',regex=False) | (df900000.Name == 'Montvila, Rev. Juozas')]\n\n60.5 ms \u00b1 853 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\n\n206 ms \u00b1 840 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\n\n\n\n\n\n%timeit df9000000.loc[df9000000.Name.s_string_contains('y') | df9000000.Name.s_string_is('Montvila, Rev. Juozas')]\n\n%timeit df9000000.loc[df9000000.Name.str.contains('y',regex=False) | (df9000000.Name == 'Montvila, Rev. Juozas')]\n\n596 ms \u00b1 11.8 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\n\n2.06 s \u00b1 2.5 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\n\n\n\n\n\n# Good news: it can handle unicode characters! \n\ndf9000.loc[df9000.Name.s_string_contains('\u00f6')].Name\n\nOut[14]: \n\n0       Braund, Mr. Owen Harris\u00f6\n\n891     Braund, Mr. Owen Harris\u00f6\n\n1782    Braund, Mr. Owen Harris\u00f6\n\n2673    Braund, Mr. Owen Harris\u00f6\n\n3564    Braund, Mr. Owen Harris\u00f6\n\n4455    Braund, Mr. Owen Harris\u00f6\n\n5346    Braund, Mr. Owen Harris\u00f6\n\n6237    Braund, Mr. Owen Harris\u00f6\n\n7128    Braund, Mr. Owen Harris\u00f6\n\n8019    Braund, Mr. Owen Harris\u00f6\n\nName: Name, dtype: string\n\n\n\n\n\n# Bad news: every time you modify a Series, you have to update it: \n\n\n\ndf9000.loc[df9000.Name.s_string_contains('\u00f6')].Name\n\n0       Braund, Mr. Owen Harris\u00f6\n\n891     Braund, Mr. Owen Harris\u00f6\n\n1782    Braund, Mr. Owen Harris\u00f6\n\n2673    Braund, Mr. Owen Harris\u00f6\n\n3564    Braund, Mr. Owen Harris\u00f6\n\n\n\n\n\ndf9000.loc[df9000.Name.s_string_contains('\u00f6'), \"Name\"] = df9000.loc[df9000.Name.s_string_contains('\u00f6'), \"Name\"] + '\u00c4' # updating \n\n\n\ndf9000.Name\n\n0                               Braund, Mr. Owen Harris\u00f6\u00c4\n\n1       Cumings, Mrs. John Bradley (Florence Briggs Th...\n\n2                                  Heikkinen, Miss. Laina\n\n\n\ndf9000.loc[df9000.Name.s_string_contains('\u00f6'), \"Name\"]  # Exception because ds_update_fast_string was not called\n\n\n\nTraceback (most recent call last):\n\n  File \"C:\\Users\\Gamer\\anaconda3\\envs\\dfdir\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 3398, in run_code\n\n    exec(code_obj, self.user_global_ns, self.user_ns)\n\n  File \"<ipython-input-7-2b0dfaf8b41c>\", line 1, in <cell line: 1>\n\n    df9000.loc[df9000.Name.s_string_contains('\u00f6'), \"Name\"]\n\n  File \"C:/Users/Gamer/anaconda3/envs/dfdir/a_pandas_string_search.py\", line 133, in search_contains\n\n    wordtosearchbin, columntosearch = _get_col_word(\n\n  File \"C:/Users/Gamer/anaconda3/envs/dfdir/a_pandas_string_search.py\", line 103, in _get_col_word\n\n    return wordtosearchbin, series._stringser.__array__()\n\nAttributeError: 'NoneType' object has no attribute '__array__'\n\n\n\ndf9000.Name.ds_update_fast_string() # Necessary after changing a Series\n\n# you can also update the whole DataFrame: df9000 = df9000.ds_update_fast_string()\n\n# Be careful: df9000.Name.ds_update_fast_string() returns None (inplace) \n\n# df9000.ds_update_fast_string() returns a DataFrame\n\n\n\ndf9000.loc[df9000.Name.s_string_contains('\u00f6'), \"Name\"]  # Now it is working!\n\n\n\n0       Braund, Mr. Owen Harris\u00f6\u00c4\n\n891     Braund, Mr. Owen Harris\u00f6\u00c4\n\n1782    Braund, Mr. Owen Harris\u00f6\u00c4\n\n2673    Braund, Mr. Owen Harris\u00f6\u00c4\n\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Up to 4x faster than Series.str.contains / Series.eq - can handle Unicode!",
    "version": "0.11",
    "split_keywords": [
        "pandas",
        "dataframe",
        "string",
        "series",
        "fast",
        "unicode"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "d594debfc4f36af74b726d632da2d42f",
                "sha256": "54ad91004bbb9ee0158a77d851ee6fd9db30c2d0d539a92ff452ef1a458b40da"
            },
            "downloads": -1,
            "filename": "a_pandas_ex_fast_string-0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d594debfc4f36af74b726d632da2d42f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7603,
            "upload_time": "2022-12-20T04:55:22",
            "upload_time_iso_8601": "2022-12-20T04:55:22.248088Z",
            "url": "https://files.pythonhosted.org/packages/b3/0a/d428ec6ae3cc6b1ff9a6c0d670aeb863cc59f72f4528961183556f8ccd96/a_pandas_ex_fast_string-0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "3bfb4c18267ed7d985e5e1b99b97e817",
                "sha256": "a4a19c7fd7b0f4fc37ddf37ecdd638e5f84f8634471ce32ed91d1114b50ca3ef"
            },
            "downloads": -1,
            "filename": "a_pandas_ex_fast_string-0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "3bfb4c18267ed7d985e5e1b99b97e817",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5971,
            "upload_time": "2022-12-20T04:55:23",
            "upload_time_iso_8601": "2022-12-20T04:55:23.762780Z",
            "url": "https://files.pythonhosted.org/packages/05/74/0e7a4707e95520e37bd4f1e3ba8eceadcdd2a7ce78ceb25d633641cea9ff/a_pandas_ex_fast_string-0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-20 04:55:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hansalemaos",
    "github_project": "a_pandas_ex_fast_string",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "numexpr",
            "specs": []
        },
        {
            "name": "pandas",
            "specs": []
        },
        {
            "name": "ujson",
            "specs": []
        }
    ],
    "lcname": "a-pandas-ex-fast-string"
}
        
Elapsed time: 0.02431s