Skip to content

BUG: "buffer source array is read-only" with tz_convert_from_utc/DatetimeArray.date #35530

@lidavidm

Description

@lidavidm
  • I have checked that this issue has not already been reported.
    There are similar issues with the same symptom
  • I have confirmed this bug exists on the latest version of pandas.
    Tested with Pandas 1.1.0
  • (optional) I have confirmed this bug exists on the master branch of pandas.
    Tested with bdcc5bf

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pyarrow as pa
import pandas as pd
import pytz
print("PyArrow:", pa.__version__)
print("Pandas:", pd.__version__)
table = pa.table([
    pa.array([
        pd.Timestamp('2014-01-01'),
    ], type=pa.timestamp("ns"))
], names=["time"])
df = table.to_pandas(self_destruct=True, split_blocks=True)
df.set_index("time", inplace=True)
df.index = df.index.tz_localize(pytz.utc)
# These are OK
print(df.index.date)
print(df.index.tz_convert("America/New_York"))
# But not this
print(df.index.tz_convert("America/New_York").date)

Problem description

The reproduction results in this exception:

Traceback (most recent call last):
  File "repro.py", line 18, in <module>
    print(df.index.tz_convert("America/New_York").date)
  File "/home/lidavidm/Code/twosigma/pandas/temp/venv/lib/python3.8/site-packages/pandas/core/indexes/extension.py", line 54, in fget
    result = getattr(self._data, name)
  File "/home/lidavidm/Code/twosigma/pandas/temp/venv/lib/python3.8/site-packages/pandas/core/arrays/datetimes.py", line 1246, in date
    timestamps = self._local_timestamps()
  File "/home/lidavidm/Code/twosigma/pandas/temp/venv/lib/python3.8/site-packages/pandas/core/arrays/datetimes.py", line 731, in _local_timestamps
    return tzconversion.tz_convert_from_utc(self.asi8, self.tz)
  File "pandas/_libs/tslibs/tzconversion.pyx", line 407, in pandas._libs.tslibs.tzconversion.tz_convert_from_utc
  File "stringsource", line 658, in View.MemoryView.memoryview_cwrapper
  File "stringsource", line 349, in View.MemoryView.memoryview.__cinit__
ValueError: buffer source array is read-only

The data is not being modified in place, so it should work with an immutable source array.

This is because tz_convert_from_utc and _tz_convert_from_utc are missing some const specifiers in Cython.

Expected Output

The .date accessor should work as expected:

[datetime.date(2013, 12, 31)]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.7.9-arch1-1
Version : #1 SMP PREEMPT Thu, 16 Jul 2020 19:34:49 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.1.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.0
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions