Skip to content

BUG: .fillna(-1) does not work in pd.eval #46471

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

Closed
2 of 3 tasks
Demetrio92 opened this issue Mar 22, 2022 · 4 comments · Fixed by #48511
Closed
2 of 3 tasks

BUG: .fillna(-1) does not work in pd.eval #46471

Demetrio92 opened this issue Mar 22, 2022 · 4 comments · Fixed by #48511
Assignees
Labels
Bug expressions pd.eval, query

Comments

@Demetrio92
Copy link

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

import pandas as pd
def test_unary(val):
    df = pd.DataFrame({'x': np.array([0.11, 0], dtype=np.float32)})
    res = df.eval(f'x=x.fillna({val})')
test_unary(1)  # works
test_unary(-1)  # fails

Issue Description

.eval seems to be having some problems with negative values. A very similar issue has been reported, summarized and closed here: #16363

However, it clearly still persists in other instances.

Expected Behavior

test_unary(-1) # works

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.9.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.13.0-35-generic
Version : #40~20.04.1-Ubuntu SMP Mon Mar 7 09:18:32 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.5
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.11.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : 2021.11.1
scipy : 1.7.1
sqlalchemy : 1.4.25
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : 0.55.0

@Demetrio92 Demetrio92 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 22, 2022
@rhshadrach rhshadrach added expressions pd.eval, query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 23, 2022
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Mar 23, 2022
@rhshadrach
Copy link
Member

Thanks for the report! Further investigations and PRs to fix are most welcome.

@Saxeman
Copy link

Saxeman commented Mar 23, 2022

take

@shyamsantoki
Copy link

take

shyamsantoki added a commit to shyamsantoki/pandas that referenced this issue Apr 16, 2022
Adding ```self.value``` resolves the issue pandas-dev#46471
shyamsantoki added a commit to shyamsantoki/pandas that referenced this issue May 3, 2022
shyamsantoki added a commit to shyamsantoki/pandas that referenced this issue Jun 18, 2022
shyamsantoki added a commit to shyamsantoki/pandas that referenced this issue Jun 18, 2022
@PierXuY
Copy link

PierXuY commented Mar 3, 2023

It can be implemented in the following ways:

import pandas as pd
import numpy as np
df = pd.DataFrame([np.nan,1,1], columns=['A'])
result = df.eval('A.fillna(@negative(1))',local_dict={'negative':lambda x:-x})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment