Skip to content
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

BUG: dtype changes when row passed to a function via DataFrame.apply() #43503

Open
2 of 3 tasks
buhtz opened this issue Sep 10, 2021 · 1 comment
Open
2 of 3 tasks

BUG: dtype changes when row passed to a function via DataFrame.apply() #43503

buhtz opened this issue Sep 10, 2021 · 1 comment
Labels
Apply Apply, Aggregate, Transform Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions

Comments

@buhtz
Copy link

buhtz commented Sep 10, 2021

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

#!/usr/bin/env python3
import pandas as pd

# 1.2.5 and 1.3.2
print(pd.__version__)

df = pd.DataFrame(data={'VAR': ['A', 'B'], 'X': range(2)})
df.VAR = df.VAR.astype('category')
df.X = df.X.astype('int8')

# Name: VAR, dtype: category
# Categories (2, object): ['A', 'B']
print(df.VAR)

# VAR    category
# X          int8
# dtype: object
print(df.dtypes)


def foo(row):
    # doing nothing
    return row

for arg in [None, 'reduce', 'expand', 'broadcast']:
    print(f'\n{arg}')
    print(df.apply(foo, axis=1, result_type='reduce').dtypes)

Issue Description

When I "iterate" on row level via DataFrame.apply(foo, axis=1) the dtype of the columns are changing.
Here in the MWE a category becomes object and an int8 becomes `int64'.

The argument result_type= seems to have no effect. The MWE checks each possible option for it.

This is the output of the MWE

1.3.2
0    A
1    B
Name: VAR, dtype: category
Categories (2, object): ['A', 'B']
VAR    category
X          int8
dtype: object

None
VAR    object
X       int64
dtype: object

reduce
VAR    object
X       int64
dtype: object

expand
VAR    object
X       int64
dtype: object

broadcast
VAR    object
X       int64
dtype: object

Expected Behavior

Just keep the dtype. Do not convert it without users permission. or minimally throw a warning if there is a good reason for it.

Possible related #42001

Installed Versions

INSTALLED VERSIONS

commit : 5f648bf
python : 3.9.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252

pandas : 1.3.2
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.2.4
setuptools : 57.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.23.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.1
pyxlsb : None
s3fs : None
scipy : 1.7.0
sqlalchemy : 1.4.13
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None

@buhtz buhtz added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 10, 2021
@mroeschke mroeschke added Apply Apply, Aggregate, Transform Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 30, 2021
@WXZhao7
Copy link

WXZhao7 commented Nov 11, 2021

I meet the similar problem when using datafram.apply(lambda x: f-string)

  • At first, I check the different formatting methods.
import pandas as pd

data = {'A': [1, 2, 3], 'B': [1.0, 2.0, 3.0]}
df = pd.DataFrame(data)
temp = df.astype({'A': int, 'B': float})
print("-- Dataframe.apply change the data type --")
print(temp.apply(lambda x: f"{x['A']}-{x['B']}", axis='columns'))
print("-- data type --")
print(temp.apply(lambda x: f"{type(x['A'])}:{type(x['B'])}", axis='columns'))
print("-- Error")
try:
    d = temp.apply(lambda x: f"{x['A']:2d}:{x['B']:2f}", axis='columns')
except Exception as E:
    print(E)
else:
    print(d)
print("\n\n-- other test")
print("--- %-formatting is ok --")
print(temp.apply(lambda x: "%2d-%2f" % (x['A'], x['B']), axis='columns'))
print("--- str.format() wrong --")
print(temp.apply(lambda x: "{}-{}".format(type(x['A']), type(x['B'])), axis='columns'))

pd.show_versions()

The result shows that f-string and str.format() are different with %-formatting

-- Dataframe.apply change the data type --
0    1.0-1.0
1    2.0-2.0
2    3.0-3.0
dtype: object
-- data type --
0    <class 'numpy.float64'>:<class 'numpy.float64'>
1    <class 'numpy.float64'>:<class 'numpy.float64'>
2    <class 'numpy.float64'>:<class 'numpy.float64'>
dtype: object
-- Error
Unknown format code 'd' for object of type 'float'


-- other test
--- %-formatting is ok --
0     1-1.000000
1     2-2.000000
2     3-3.000000
dtype: object
--- str.format() wrong --
0    <class 'numpy.float64'>-<class 'numpy.float64'>
1    <class 'numpy.float64'>-<class 'numpy.float64'>
2    <class 'numpy.float64'>-<class 'numpy.float64'>
dtype: object

INSTALLED VERSIONS
------------------
commit           : 945c9ed766a61c7d2c0a7cbb251b6edebf9cb7d5
python           : 3.9.7.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.11.0-40-generic
Version          : #44~20.04.2-Ubuntu SMP Tue Oct 26 18:07:44 UTC 2021
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.3.4
numpy            : 1.21.4
pytz             : 2021.3
dateutil         : 2.8.2
pip              : 21.3.1
setuptools       : 58.5.3
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : 4.6.4
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : 7.29.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.4.3
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : 1.7.2
sqlalchemy       : 1.4.26
tables           : None
tabulate         : None
xarray           : 0.20.1
xlrd             : None
xlwt             : None
numba            : None

Then I do some tests, I find this may be caused by the data type change during the Series obtained from Dataframe.

f = lambda x: f"{type(x['A'])}:{type(x['B'])}"
x = df.iloc[0,]
print("--- [check] lambda function")
print(f(x))
print("--- [check] get a series from dataframe")
print(">>> type(df.iloc[0]['A'])")
print(type(df.iloc[0]['A']))
print(">>> type(df.loc[0,'A'])")
print(type(df.loc[0, 'A']))
--- [check] lambda function
<class 'numpy.float64'>:<class 'numpy.float64'>
--- [check] get a series from dataframe
>>> type(df.iloc[0]['A'])
<class 'numpy.float64'>
>>> type(df.loc[0,'A'])
<class 'numpy.int64'>

Because df.loc[0,'A'] and df.iloc[0]['A'] show different data type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

No branches or pull requests

3 participants