Repository: pandas-dev/pandas

Issue:
Pickled TimedeltaIndex from 0.19.2 cannot be unpickled in 0.20+ using pd.read_pickle
#### Code Sample, a copy-pastable example if possible

```python
# On pandas 0.19.2, python 2.7
import pandas as pd
import cPickle as pickle

td30 = pd.timedelta_range('00:30:00', '01:30:00', freq='30T')
td30.to_series().to_pickle('C:/temp/td30.pkl')        

pd.read_pickle('c:/temp/td30.pkl')

Out[7]: 
00:30:00   00:30:00
01:00:00   01:00:00
01:30:00   01:30:00
Freq: 30T, dtype: timedelta64[ns]


# On latest master
In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '0.23.0.dev0+394.gf4c9d96'

In [3]: pd.read_pickle('C:/temp/td30.pkl')
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
C:\projects\pandas-dk\pandas\io\pickle.py in try_read(path, encoding)
    101                 # We want to silencce any warnings about, e.g. moved modules.
--> 102                 return read_wrapper(lambda f: pkl.load(f))
    103         except Exception:

C:\projects\pandas-dk\pandas\io\pickle.py in read_wrapper(func)
     84         try:
---> 85             return func(f)
     86         finally:

C:\projects\pandas-dk\pandas\io\pickle.py in <lambda>(f)
    101                 # We want to silencce any warnings about, e.g. moved modules.
--> 102                 return read_wrapper(lambda f: pkl.load(f))
    103         except Exception:

ModuleNotFoundError: No module named 'pandas.indexes.base'



```
#### Problem description

Objects with TimedeltaIndexes created under pandas 0.19.2 cannot be unpickled using `pandas.read_pickle` on later versions of pandas. I have found the same behavior on python 2.7 and 3.6, with pandas 0.20.3 through latest master. On Python 3.6  it raises ModuleNotFoundError: No module named 'pandas.indexes.base'; under python 2.7, it raises ImportError saying "No module named tdi".

Apologies if this is a dupe; while I have seen other reports of problems unpickling objects created in 0.19.2, it appears all could be fixed by using `read_pickle`, while here using `read_pickle` does not solve the problem.

#### Expected Output

Correct unpickling without error. Given the above example, should be the following under 0.23 master:

00:30:00   00:30:00
01:00:00   01:00:00
01:30:00   01:30:00
Freq: 30T, dtype: timedelta64[ns]

#### Output of ``pd.show_versions()``

<details>
Creating the pickle:

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.14.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 62 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.25.2
numpy: 1.11.3
scipy: 0.19.0
statsmodels: 0.8.0
xarray: 0.9.6
IPython: 5.3.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.8.0
bs4: 4.6.0
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
boto: 2.48.0
pandas_datareader: 0.5.0

Reading the pickle (master):

INSTALLED VERSIONS
------------------
commit: f4c9d966fb20d9ca3221d31bd9eb31117065808c
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 62 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0.dev0+394.gf4c9d96
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: 0.1.2
fastparquet: 0.1.4
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:
Pickle compat is at https://github.com/pandas-dev/pandas/blob/master/pandas/compat/pickle_compat.py

May be as simple as adding an item to https://github.com/pandas-dev/pandas/blob/6ef4be3f8f269f147b5abedecf7da6f19af305d3/pandas/compat/pickle_compat.py#L58, or it may be harder :)
Thanks for the pointer @TomAugspurger. Looks like TimedeltaIndex was missing at 112 of pickle_compat.py. Adding it seems to fix the issue.

I'm also seeing issues with MultiIndexes with DatetimeIndex levels. I'll see if it's related and might combine into one PR.

Resolving patch:
diff --git a/pandas/compat/pickle_compat.py b/pandas/compat/pickle_compat.py
--- a/pandas/compat/pickle_compat.py
+++ b/pandas/compat/pickle_compat.py
@@ -113,6 +113,12 @@ def load_reduce(self):
     # 19269, arrays moving
     ('pandas.core.categorical', 'Categorical'):
         ('pandas.core.arrays', 'Categorical'),
+
+    # 19939, add timedeltaindex, float64index compat from 15998 move
+    ('pandas.tseries.tdi', 'TimedeltaIndex'):
+        ('pandas.core.indexes.timedeltas', 'TimedeltaIndex'),
+    ('pandas.indexes.numeric', 'Float64Index'):
+        ('pandas.core.indexes.numeric', 'Float64Index'),
 }
 
 


Test patch:
diff --git a/pandas/tests/io/data/legacy_pickle/0.16.2/0.16.2_AMD64_windows_2.7.14.pickle b/pandas/tests/io/data/legacy_pickle/0.16.2/0.16.2_AMD64_windows_2.7.14.pickle
new file mode 100644
Binary files /dev/null and b/pandas/tests/io/data/legacy_pickle/0.16.2/0.16.2_AMD64_windows_2.7.14.pickle differ
diff --git a/pandas/tests/io/data/legacy_pickle/0.19.2/0.19.2_AMD64_windows_2.7.14.pickle b/pandas/tests/io/data/legacy_pickle/0.19.2/0.19.2_AMD64_windows_2.7.14.pickle
new file mode 100644
Binary files /dev/null and b/pandas/tests/io/data/legacy_pickle/0.19.2/0.19.2_AMD64_windows_2.7.14.pickle differ
diff --git a/pandas/tests/io/generate_legacy_storage_files.py b/pandas/tests/io/generate_legacy_storage_files.py
--- a/pandas/tests/io/generate_legacy_storage_files.py
+++ b/pandas/tests/io/generate_legacy_storage_files.py
@@ -40,7 +40,7 @@
 from pandas import (Series, DataFrame, Panel,
                     SparseSeries, SparseDataFrame,
                     Index, MultiIndex, bdate_range, to_msgpack,
-                    date_range, period_range,
+                    date_range, period_range, timedelta_range,
                     Timestamp, NaT, Categorical, Period)
 from pandas.tseries.offsets import (
     DateOffset, Hour, Minute, Day,
@@ -116,7 +116,18 @@ def create_data():
 
     index = dict(int=Index(np.arange(10)),
                  date=date_range('20130101', periods=10),
-                 period=period_range('2013-01-01', freq='M', periods=10))
+                 period=period_range('2013-01-01', freq='M', periods=10),
+                 float=Index(np.arange(10, dtype=np.float64)),
+                 uint=Index(np.arange(10, dtype=np.uint64)),
+                 timedelta=timedelta_range('00:00:00', freq='30T', periods=10))
+
+    if _loose_version >= LooseVersion('0.18'):
+        from pandas import RangeIndex
+        index['range'] = RangeIndex(10)
+
+    if _loose_version >= LooseVersion('0.21'):
+        from pandas import interval_range
+        index['interval'] = interval_range(0, periods=10)
 
     mi = dict(reg2=MultiIndex.from_tuples(
         tuple(zip(*[[u'bar', u'bar', u'baz', u'baz', u'foo',
@@ -276,6 +287,8 @@ def create_msgpack_data():
     del data['frame']['cat_onecol']
     del data['frame']['cat_and_float']
     del data['scalars']['period']
+    del data['index']['interval']
+    del data['offsets']
     return _u(data)