Skip to content

BUG: Segfault when calling df.to_json(orient='records') and numpy.datetime64 being serialized #58160

Closed
@paddymul

Description

@paddymul

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

Reproducing this bug has been very difficult.  I have a test script that runs off of my library repo that gets it to fail, but when trying to make a reduced testcase it doesn't fail.  Given that i'm filing this against an old version of pandas, I'll just describe the bug to start.  I'm happy to work with you to produce a reduced test case.

This is the code that should cause a segfault

ts = pd.Timestamp('2020-02-21T00:00:00.000000000')
np_dt = pd.Series([ts]).mode().values[0]
sdf = pd.DataFrame({'a':[ts], 'b':[np_dt]}, index=['mode'])
sdf.to_json(orient='table', indent=2, default_handler=str)


My actual code in the library (with print instrumentation)
```python
def pd_to_obj(df:pd.DataFrame):
    print("--------------")
    print("here ser 86")

    if len(df) > 4:
        df = df[20:21]
        print(df)
        val = df.iloc[0][0]
        print("val", val)
        print("type(val)", type(val))

    print("df.index", df.index)
    print("len df", len(df))
    serialized = df.to_json(orient='table')
    print("here ser 88")

this fails at the df.to_json call. Here is the debugging info outputted

--------------
here ser 86
                          EventDate
mode  2020-02-21T00:00:00.000000000
val 2020-02-21T00:00:00.000000000
type(val) <class 'numpy.datetime64'>
df.index Index(['mode'], dtype='object')
len df 1
zsh: segmentation fault  python crash_buckaroo.py


### Issue Description

Pandas 2.0.3 segafaults when executing `to_json(orient='records')`

### Expected Behavior

There shouldn't be a segfault

### Installed Versions

<details>

INSTALLED VERSIONS
------------------
commit           : 0f437949513225922d851e9581723d82120684a6
python           : 3.10.14.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 22.6.0
Version          : Darwin Kernel Version 22.6.0: Wed Oct  4 21:25:40 PDT 2023; root:xnu-8796.141.3.701.17~4/RELEASE_ARM64_T8103
machine          : arm64
processor        : arm
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 2.0.3
numpy            : 1.26.4
pytz             : 2024.1
dateutil         : 2.9.0.post0
setuptools       : 68.2.2
pip              : 23.3.1
Cython           : None
pytest           : 8.1.1
hypothesis       : None
sphinx           : 7.2.6
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.3
IPython          : 8.23.0
pandas_datareader: None
bs4              : 4.12.3
bottleneck       : None
brotli           : None
fastparquet      : None
fsspec           : None
gcsfs            : None
matplotlib       : 3.8.4
numba            : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : 1.13.0
snappy           : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
zstandard        : None
tzdata           : 2024.1
qtpy             : None
pyqt5            : None

</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions