Pandas version checks

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

  • [X] 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
import numpy as np

print(pd.__version__, np.__version__)

df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                   'weapon': [22.5, np.NAN],
                   'mask': ['red', 'purple'],
                   })

df.to_csv('asdf.csv')

Issue Description

Running the above code runs just fine when numpy < 1.24 is used.

Using the latest version of numpy, it's causing a RuntimeWarning on to_csv (due to the NaN).

site-packages/pandas/core/internals/blocks.py:2323: RuntimeWarning: invalid value encountered in cast
  values = values.astype(str)

Expected Behavior

No runtime error when using the latest numpy>=1.24

Installed Versions

INSTALLED VERSIONS ------------------ commit : 8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7 python : 3.10.8.final.0 python-bits : 64 OS : Linux OS-release : 6.0.12-arch1-1 Version : #1 SMP PREEMPT_DYNAMIC Thu, 08 Dec 2022 11:03:38 +0000 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 1.5.2 numpy : 1.24.0 pytz : 2022.6 dateutil : 2.8.2 setuptools : 63.2.0 pip : 22.3.1 Cython : None pytest : 7.2.0 hypothesis : None sphinx : None blosc : 1.11.1 feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : 2.9.5 jinja2 : 3.1.2 IPython : 8.6.0 pandas_datareader: None bs4 : 4.11.1 bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.6.2 numba : None numexpr : 2.8.4 odfpy : None openpyxl : None pandas_gbq : None pyarrow : 10.0.1 pyreadstat : None pyxlsb : None s3fs : None scipy : 1.9.3 snappy : None sqlalchemy : 1.4.45 tables : 3.7.0 tabulate : 0.9.0 xarray : None xlrd : None xlwt : None zstandard : None tzdata : 2022.6

Comment From: phofl

Hi, thanks for your report. I can't reproduce on Mac. I checked your ci where this is actually raising. Could you maybe post a reproducible example for this?

Comment From: xmatthias

@phofl the above sample does reproduce the runtime error reliably for me - not sure what else you need?

if it's not showing - are you sure your numpy is 1.24.0 ? above reproduction:

Pandas BUG: RuntimeWarning when using .to_csv with NAN on latest numpy>=1.24

Comment From: asishm

xref: https://github.com/numpy/numpy/issues/22843

Comment From: phofl

Yeah checked multiple times and in 2 different environments, but closing here since it’s fixed in numpy

Comment From: asishm

fwiw - this consistently reproduces for me with pd.DataFrame({'a': ['foo'], 'b': [np.nan]}).to_csv()

In [55]: pd.DataFrame({'a': ['foo'], 'b': [np.nan]}).to_csv()
/home/asishm/pandas-asishm/pandas/core/internals/blocks.py:2239: RuntimeWarning: invalid value encountered in cast
  values = values.astype(str)
Out[55]: ',a,b\n0,foo,\n'
In [57]: pd.__version__, np.__version__
Out[57]: ('2.0.0.dev0+971.gca3e0c875f', '1.24.0')

on wsl2