Skip to content
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

BUG: Return KeyError for invalid string key #23540

Merged

Conversation

toobaz
Copy link
Member

@toobaz toobaz commented Nov 7, 2018

In principle, it is nice to raise different errors if the string is invalid, or if the resulting period is missing. However, we want, and need, methods such as .loc to return errors of predictable types.
And indeed, this is how PeriodIndex, for instance, works:

In [2]: pd.date_range('2010-10-10', '2010-10-11').get_loc('pippo')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

[...]

/home/nobackup/repo/pandas/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1019                 if 'list-like' in str(e):
   1020                     raise e
-> 1021                 raise KeyError(key)
   1022 
   1023     def _maybe_cast_slice_bound(self, label, side, kind):

KeyError: 'pippo'

@toobaz toobaz added Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type labels Nov 7, 2018
@pep8speaks
Copy link

Hello @toobaz! Thanks for submitting the PR.

@toobaz
Copy link
Member Author

toobaz commented Nov 7, 2018

@Licht-T

@toobaz toobaz force-pushed the period_index_dateparseerror branch from fae8820 to e44b249 Compare November 7, 2018 08:30
@codecov
Copy link

codecov bot commented Nov 7, 2018

Codecov Report

Merging #23540 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #23540      +/-   ##
==========================================
+ Coverage   92.25%   92.25%   +<.01%     
==========================================
  Files         161      161              
  Lines       51207    51212       +5     
==========================================
+ Hits        47239    47244       +5     
  Misses       3968     3968
Flag Coverage Δ
#multiple 90.63% <100%> (ø) ⬆️
#single 42.28% <14.28%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/period.py 92.25% <100%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 528527b...e44b249. Read the comment docs.

@jreback jreback added this to the 0.24.0 milestone Nov 7, 2018
@jreback
Copy link
Contributor

jreback commented Nov 7, 2018

lgtm.

@TomAugspurger TomAugspurger merged commit a76bf63 into pandas-dev:master Nov 7, 2018
@TomAugspurger
Copy link
Contributor

Thanks!

@toobaz toobaz deleted the period_index_dateparseerror branch November 7, 2018 14:17
thoo added a commit to thoo/pandas that referenced this pull request Nov 10, 2018
…fixed

* upstream/master: (47 commits)
  CLN: remove values attribute from datetimelike EAs (pandas-dev#23603)
  DOC/CI: Add linting to rst files, and fix issues (pandas-dev#23381)
  PERF: Speeds up creation of Period, PeriodArray, with Offset freq (pandas-dev#23589)
  PERF: define is_all_dates to shortcut inadvertent copy when slicing an IntervalIndex (pandas-dev#23591)
  TST: Tests and Helpers for Datetime/Period Arrays (pandas-dev#23502)
  Update description of Index._values/values/ndarray_values (pandas-dev#23507)
  Fixes to make validate_docstrings.py not generate warnings or unwanted output (pandas-dev#23552)
  DOC: Added note about groupby excluding Decimal columns by default (pandas-dev#18953)
  ENH: Support writing timestamps with timezones with to_sql (pandas-dev#22654)
  CI: Auto-cancel redundant builds (pandas-dev#23523)
  Preserve EA dtype in DataFrame.stack (pandas-dev#23285)
  TST: Fix dtype mismatch on 32bit in IntervalTree get_indexer test (pandas-dev#23468)
  BUG: raise if invalid freq is passed (pandas-dev#23546)
  remove uses of (ts)?lib.(NaT|iNaT|Timestamp) (pandas-dev#23562)
  BUG: Fix error message for invalid HTML flavor (pandas-dev#23550)
  ENH: Support EAs in Series.unstack (pandas-dev#23284)
  DOC: Updating DataFrame.join docstring (pandas-dev#23471)
  TST: coverage for skipped tests in io/formats/test_to_html.py (pandas-dev#22888)
  BUG: Return KeyError for invalid string key (pandas-dev#23540)
  BUG: DatetimeIndex slicing with boolean Index raises TypeError (pandas-dev#22852)
  ...
JustinZhengBC pushed a commit to JustinZhengBC/pandas that referenced this pull request Nov 14, 2018
tm9k1 pushed a commit to tm9k1/pandas that referenced this pull request Nov 19, 2018
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this pull request Feb 28, 2019
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this pull request Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indexing into a series with multi-index containing periods raises an exception
5 participants