Skip to content

BUG: DataFrame.mask does not mask NaT using None #41303

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
StefanBrand opened this issue May 4, 2021 · 3 comments
Closed
2 of 3 tasks

BUG: DataFrame.mask does not mask NaT using None #41303

StefanBrand opened this issue May 4, 2021 · 3 comments
Labels
Duplicate Report Duplicate issue or pull request

Comments

@StefanBrand
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd
import numpy as np
df = pd.DataFrame({"dates": ["2020-07-07", np.datetime64("Nat")]})
df.mask(df.isna(), None)

Problem description

df.mask does not replace NaT by None.

image

Expected Output

df.mask should properly mask NaT.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 2cb96529396d93b46abab7bbc73a208e708c642e
python           : 3.8.8.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.14.214-160.339.amzn2.x86_64
Version          : #1 SMP Sun Jan 10 05:53:05 UTC 2021
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : en_US.UTF-8
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.4
numpy            : 1.20.1
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 49.6.0.post20210108
Cython           : 0.29.22
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : 4.6.2
html5lib         : None
pymysql          : None
psycopg2         : 2.8.6 (dt dec pq3 ext lo64)
jinja2           : 2.11.3
IPython          : 7.21.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 0.8.7
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.4
numexpr          : 2.7.3
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 3.0.0
pyxlsb           : None
s3fs             : 0.5.2
scipy            : 1.6.1
sqlalchemy       : 1.3.23
tables           : 3.6.1
tabulate         : None
xarray           : 0.17.0
xlrd             : 1.2.0
xlwt             : None
numba            : 0.52.0
@StefanBrand StefanBrand added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 4, 2021
@StefanBrand StefanBrand changed the title BUG: DataFrame.mask does not mask NaT BUG: DataFrame.mask does not mask NaT using None May 4, 2021
@StefanBrand
Copy link
Author

Seems to be the same issue as this: #15613

@StefanBrand
Copy link
Author

My usecase is encoding as JSON:

def default(o):
    try:
        isodate = o.date().isoformat()
    except AttributeError:
        pass
    else:
        if isodate == "NaT":
            return None
        return isodate
#   Let the base class default method raise the TypeError
    return json.JSONEncoder.default(self, o)

json.JSONEncoder(default=default).encode(df.to_dict())

{"dates": {"0": "2020-07-07", "1": null}}

I just found out that there is df.to_json(), which sort of does the same:

df.to_json(date_format="iso", date_unit="s")

{"dates":{"0":"2020-07-07T00:00:00Z","1":null}}

I only wish I could set the date_unit precision to "day"...

@jreback jreback added Duplicate Report Duplicate issue or pull request and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 4, 2021
@jreback jreback added this to the No action milestone May 4, 2021
@jreback
Copy link
Contributor

jreback commented May 4, 2021

closing as duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants