Skip to content

Series.__setitem__ with datetime64[ns, tz] dtype raises ValueError when setting a tz-aware timestamps #12862

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
jluttine opened this issue Apr 11, 2016 · 2 comments · Fixed by #27322
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Timezones Timezone data dtype
Milestone

Comments

@jluttine
Copy link

I'm not able to construct a Series element by element if the elements are timezone localized datetimes. Any workarounds?

Code Sample, a copy-pastable example if possible

# This works
x = pd.Series()
x['foo'] = pd.to_datetime(42)
x['bar'] = pd.to_datetime(666)

# This doesn't
x = pd.Series()
x['foo'] = pd.to_datetime(42).tz_localize('UTC')
x['bar'] = pd.to_datetime(666).tz_localize('UTC')
ValueError                                Traceback (most recent call last)
<ipython-input-24-cc1ab78086e5> in <module>()
      1 x = pd.Series()
      2 x['foo'] = pd.to_datetime(42).tz_localize('UTC')
----> 3 x['bar'] = pd.to_datetime(666).tz_localize('UTC')

/usr/lib/python3.5/site-packages/pandas/core/series.py in __setitem__(self, key, value)
    726         # do the setitem
    727         cacher_needs_updating = self._check_is_chained_assignment_possible()
--> 728         setitem(key, value)
    729         if cacher_needs_updating:
    730             self._maybe_update_cacher()

/usr/lib/python3.5/site-packages/pandas/core/series.py in setitem(key, value)
    722                     pass
    723 
--> 724             self._set_with(key, value)
    725 
    726         # do the setitem

/usr/lib/python3.5/site-packages/pandas/core/series.py in _set_with(self, key, value)
    770                 self._set_values(key.astype(np.bool_), value)
    771             else:
--> 772                 self._set_labels(key, value)
    773 
    774     def _set_labels(self, key, value):

/usr/lib/python3.5/site-packages/pandas/core/series.py in _set_labels(self, key, value)
    780         mask = indexer == -1
    781         if mask.any():
--> 782             raise ValueError('%s not contained in the index' % str(key[mask]))
    783         self._set_values(indexer, value)
    784 

ValueError: ['b' 'a' 'r'] not contained in the index

Expected Output

Shouldn't raise errors.

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.6-gnu-1
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_DK.UTF-8

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.1
setuptools: 18.2
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: 1.4
patsy: None
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: None
xlrd: 0.9.4
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: None
httplib2: 0.9.2
apiclient: 1.5.0
sqlalchemy: 1.0.12
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext lo64)
jinja2: 2.8
boto: None
@jreback
Copy link
Contributor

jreback commented Apr 11, 2016

you can do x.loc['bar'] = ....

bug on the getitem expansion though.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves Timezones Timezone data dtype Difficulty Intermediate labels Apr 11, 2016
@jreback jreback added this to the Next Major Release milestone Apr 11, 2016
@mroeschke mroeschke changed the title Unable to construct Series for localized datetimes Series.__setitem__ raises ValueError when setting tz-aware timestamps Jul 26, 2018
@mroeschke mroeschke changed the title Series.__setitem__ raises ValueError when setting tz-aware timestamps Series.__setitem__ raises ValueError when setting multiple tz-aware timestamps Jul 26, 2018
@mroeschke mroeschke changed the title Series.__setitem__ raises ValueError when setting multiple tz-aware timestamps Series.__setitem__ with datetime64[ns, tz] dtype raises ValueError when setting a tz-aware timestamps Jul 26, 2018
@jbrockmendel jbrockmendel self-assigned this Jan 28, 2019
@jreback jreback modified the milestones: Contributions Welcome, 0.24.2, 0.25.0 Feb 16, 2019
@TomAugspurger
Copy link
Contributor

Pushing to 0.25.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Timezones Timezone data dtype
Projects
None yet
4 participants