DEPR: Add Deprecated warning for timedelta with passed units M and Y 
State: closed

- [x] closes #16344
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] tests passed locally - `pytest  pandas/tests/scalar/timedelta/test_timedelta.py -k test_unit_m_y_deprecated` and `pytest  pandas/tests/indexes/timedeltas/test_timedelta.py -k test_unit_m_y_deprecated`
- [x] checked import format to pass linter - `isort`

Labels:
name: Timedelta
description: Timedelta data type
name: Deprecate
description: Functionality to remove in pandas

Comments:
Hello @ryankarlos! Thanks for updating the PR.









Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on February 08, 2019 at 22:56 Hours UTC
Just made a related PR #23259 for timedelta docstring so thought I would give this a try as well. This is an initial PR to check if Im on the right track - my first time adding deprecated warnings.


Could you add some tests that validate that the warning is raised? Would need to check `Timedelta`, `TimedeltaIndex`, and `to_timedelta`.

Also there might be some existing tests that use those offsets, so could you check the logs and see if any tests are raising the warning as well?
Sure will work on it. 
Deprecating `Timedelta` may be easier after #23439
@mroeschke Ive added an update with tests for deprecating units - please have a look. Ive fetched the latest merge #23439 to include the changes from there as well.
Be sure to also check the CI runs (travis) to see where else 'M' and 'Y' might be used with timedelta. We will need to catch the deprecation behavior of other tests that use this behavior.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=h1) Report
> Merging [#23264](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/1d1b14c7ea7c3e497b66453cacbb8230b26cb6a6?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/23264/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #23264      +/-   ##
==========================================
- Coverage   92.15%   92.15%   -0.01%     
==========================================
  Files         166      166              
  Lines       52294    52299       +5     
==========================================
+ Hits        48194    48198       +4     
- Misses       4100     4101       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.62% <100%> (ø)` | :arrow_up: |
| #single | `42.27% <60%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/tools/timedeltas.py](https://codecov.io/gh/pandas-dev/pandas/pull/23264/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvdG9vbHMvdGltZWRlbHRhcy5weQ==) | `98.11% <100%> (+0.11%)` | :arrow_up: |
| [pandas/core/indexes/timedeltas.py](https://codecov.io/gh/pandas-dev/pandas/pull/23264/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy90aW1lZGVsdGFzLnB5) | `90.27% <100%> (+0.05%)` | :arrow_up: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/23264/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `87.89% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=footer). Last update [1d1b14c...76497d3](https://codecov.io/gh/pandas-dev/pandas/pull/23264?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@mroeschke sorry for the delay - ive added those fixes in now.  I could see around 6 failures in Travis - showing deprecation warnings for `test_unit_parser` using M and Y.  
can you merge master and upate
@jreback Yes sure, sorry somehow I had missed your replies to my previous commits.
@mroeschke  I think I may need to add a warning for Timedelta as its not producing any FutureWarning making the tests fail  - im not sure where this would need to be added though
Sorry for the delay @ryankarlos. 

You can add the `FutureWarning` here for the `Timedelta` scalar:

https://github.com/pandas-dev/pandas/blob/7e64d9c99b14f6596bf1b72e25a7cb451d0159f1/pandas/_libs/tslibs/timedeltas.pyx#L1143 
Made the changes, however travis is throwing an error for the `stacklevel` in `warnings.warn` for `Timedelta` specifically,  even though I've set it as 2 like for `to_timedelta`, `TimedeltaIndex`.
@jreback Added `stacklevel=3` for all the warnings but not sure why Travis is still showing a  `stacklevel` error.
@ryankarlos does it pass for you locally?
@ryankarlos prob can add ``check_stacklevel=False`` to the suspect tests if it passes for you locally.

pls merge master as well.
@jreback @TomAugspurger  So I now made `stacklevel=2` and ran it locally.  `to_timedelta` and `TimedeltaIndex` pass but not `Timedelta`. Even replacing `check_stacklevel=False` for Timdelta made no difference. Ive pasted the failed and passed outputs below: 



%%%%%%%%%%%%%%`Timedelta` failed output :  %%%%%%%%%%%%%%%%%%%%%

```
================================================================= FAILURES =================================================================
________________________________________________TestTimedeltas.test_unit_m_y_deprecated[Y]________________________________________________`

self = <pandas.tests.scalar.timedelta.test_timedelta.TestTimedeltas object at 0x11f446fd0>, unit = 'Y'

    @pytest.mark.parametrize('unit', ['Y'])
    def test_unit_m_y_deprecated(self, unit):
        with tm.assert_produces_warning(FutureWarning) as w:
>           Timedelta(10, unit)

scalar/timedelta/test_timedelta.py:381: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ 

>   ???
E   FutureWarning: M and Y units are deprecated and will be removed in a future version.

pandas/_libs/tslibs/timedeltas.pyx:1192: FutureWarning
================================================= 1 failed, 75 deselected in 0.60 seconds ==================================================
```

.
.
.
.
.

%%%%%%%%%%%%%%%%%`TimedeltaIndex` passes: %%%%%%%%%%%%%%%%%%%%%%%%

```
=========================================================== test session starts` ============================================================`
platform darwin -- Python 3.6.6, pytest-3.8.2, py-1.6.0, pluggy-0.7.1
rootdir: /Users/ryannazareth/Documents/Python_sprints/pandas-ryankarlos, inifile: setup.cfg
plugins: hypothesis-3.68.0
collected 100 items / 97 deselected                                                                                                        

indexes/timedeltas/test_timedelta.py ...                                                                                             [100%]

================================================= 3 passed, 97 deselected in 0.49
seconds ==================================================
```
I haven't looked too closely, but you aren't going to get the stacklevel correct for both `to_timedelta` and `Timedelta`. You'll need to pick one to target, and set `check_stacklevel=False` for the other test.

Did you recompile the C extensions before running your tests in 
https://github.com/pandas-dev/pandas/pull/23264#issuecomment-456899364?
Thanks, yes recompiling C extensions fixed the errors !
@jorisvandenbossche Decorating one of the functions upstream with ` @pytest.mark.filterwarnings` suppressed one of the future warnings generated from the other tests but not sure what do for some of the pyarrow warnings in 2.7 builds generated before my warnings are picked up.
If it is only failing to catch the warning for the python 2.7 builds, I would just skip it there (we are going to remove python 2.7 support anyhow before the next release is done from master)
@jreback all green now after skipping tests for the 2.7 builds. @jorisvandenbossche I’ve also reverted those changes as discussed at the europandas sprint yesterday. 
@jorisvandenbossche i've pushed latest changes with removing checkstacklevel for timedelta test as requested-- if it fails is it ok to revert ?
@ryankarlos tested it locally, and apparently it needs to be 1 for Timedelta, and not 2.
I just tried with 1 for Timedelta locally but still getting the assertion error
Did you rebuild pandas before testing? The change is in a cython `.pyx` file, so you need to rebuild the C extensions before the change will be in effect.
thanks, Ok it passes now but I had to merge master and build C extensions on a fresh pandas build - wasn't working when cythonizing on the previous env for some reason.
@jreback @jorisvandenbossche all green now
@jreback All green now...... I merged master and added the requested changes.