ENH: Add an errors flag to tz_localize
State: closed

closes #13057

Author: dancsi szilagyi.d@gmail.com


Labels:
name: Missing-data
description: np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
name: Timezones
description: Timezone data dtype

Comments:
You need to add some tests to test this code path.

## [Current coverage](https://codecov.io/gh/pydata/pandas/pull/13058?src=pr) is **84.14%**

> Merging [#13058](https://codecov.io/gh/pydata/pandas/pull/13058?src=pr) into [master](https://codecov.io/gh/pydata/pandas/branch/master?src=pr) will not change coverage

``` diff
@@             master     #13058   diff @@
==========================================
  Files           137        137          
  Lines         50261      50261          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
  Hits          42288      42288          
  Misses         7973       7973          
  Partials          0          0          
```

> Powered by [Codecov](https://codecov.io?src=pr). Last updated by [1296ab3...db9723b](https://codecov.io/gh/pydata/pandas/compare/1296ab39d32acaf2c77ed0c185fafe4ebcfedcb3...db9723b020a54f4dad7c5fe9340bc13642a71cd7)

pls add some tests.

If needed, I will add some tests for DatetimeIndex tomorrow...

@jreback hey, I think I discovered a bug while writing more detailed test cases for Timestamp.tz_localize. Namely, for some US timezones, the up to 7am local time on the day DST has started, the time is shifted by 1 hour in advance

``` python
>>> pd.Timestamp('2014-03-09 06:00', tz='US/Eastern')
Timestamp('2014-03-09 07:00:00-0400', tz='US/Eastern')
>>> pd.Timestamp('2015-03-08 06:01', tz='US/Eastern')
Timestamp('2015-03-08 07:01:00-0400', tz='US/Eastern')
>>> pd.Timestamp('2014-03-09 06:00', tz='US/Pacific')
Timestamp('2014-03-09 07:00:00-0700', tz='US/Pacific')
```

Note that this doesn't happen after 7am that day, or in the following days

``` python
>>> pd.Timestamp('2014-04-09 06:00', tz='US/Eastern')
Timestamp('2014-04-09 06:00:00-0400', tz='US/Eastern')
>>> pd.Timestamp('2015-04-08 06:01', tz='US/Eastern')
Timestamp('2015-04-08 06:01:00-0400', tz='US/Eastern')
>>> pd.Timestamp('2014-04-09 06:00', tz='US/Pacific')
Timestamp('2014-04-09 06:00:00-0700', tz='US/Pacific')
```

Am I missing something, or is this really a bug? I have pushed my tests. Here is `pd.show_versions()`

```
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en

pandas: 0.18.1+10.g49f3618.dirty
nose: 1.3.7
pip: 8.1.1
setuptools: 20.9.0
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: 0.7.2
IPython: 4.2.0
sphinx: 1.4.1
patsy: None
dateutil: 2.5.3
pytz: 2016.3
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None
```

@dancsi that last is this bug: https://github.com/pydata/pandas/issues/7825

when a `tz` is passed directly to the `Timestamp` constructor it is not handling DST correcty. All the other routines (meaning `pd.to_datetime`), and conversion of a naive timestamp which is then localized are fine. Its the case of a direct construction.

Well for a PR on that. (its independent of this issue).

@jreback so, I should make a PR with these tests for issue #7825, and remove them from here?

well for that issue you can add your examples, but also take the examples from that issue as well.

This PR should be independent of the other issue. IOW your tests should be testing this specific issue.

I don't understand why codecov is failing? After all, I added some tests, and they are passing...

you have to rebase against master, I fixed the .yml file.

looks pretty good. couple of commets. pls rebase / squash. ping on green.

@jreback all checks have passed. Thank you for guiding me through the process :)

very nice @dancsi thank you!

if you would like to work on other issues would be great!