Skip to content

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

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
3 tasks done
lidavidm opened this issue Aug 3, 2020 · 1 comment · Fixed by #35532
Closed
3 tasks done
Labels
Bug Datetime Datetime data dtype Timezones Timezone data dtype
Milestone

Comments

@lidavidm
Copy link
Contributor

lidavidm commented Aug 3, 2020

  • 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

@lidavidm lidavidm added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2020
@jorisvandenbossche
Copy link
Member

@lidavidm thanks for the report!

A reproducer without pyarrow:

import pandas as pd
import pytz

arr = pd.array([pd.Timestamp('2014-01-01')]).to_numpy()
arr.flags.writeable = False 
df = pd.DataFrame(index=pd.DatetimeIndex(arr, name="time"))    

df.index = df.index.tz_localize(pytz.utc)
df.index.tz_convert("America/New_York").date

@jorisvandenbossche jorisvandenbossche added Datetime Datetime data dtype Timezones Timezone data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2020
@jreback jreback added this to the 1.2 milestone Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants