Repository: pandas-dev/pandas

Issue:
BUG: nested dict construction timedelta
Related to #10160 / PR https://github.com/pydata/pandas/pull/10269

```
In [2]: from datetime import timedelta
...: from pandas import * 
...: td_as_int = [1, 2, 3, 4]
...: 
...: def create_data(constructor):
...:     return dict((i, {constructor(s): 2*i}) for i, s in enumerate(td_as_int))
...: 
...: data_timedelta64 = create_data(lambda x: np.timedelta64(x, 'D'))
...: data_timedelta = create_data(lambda x: timedelta(days=x))
...: 
...: result_timedelta64 = DataFrame(data_timedelta64)
...: result_timedelta = DataFrame(data_timedelta)
...: 

In [3]: result_timedelta64
Out[3]: 
        0   1   2   3
1 days   0 NaN NaN NaN
2 days NaN   2 NaN NaN
3 days NaN NaN   4 NaN
4 days NaN NaN NaN   6

In [4]: result_timedelta
Out[4]: 
        0   1   2   3
1 days NaN NaN NaN NaN
2 days NaN NaN NaN NaN
3 days NaN NaN NaN NaN
4 days NaN NaN NaN NaN
```

This seems to happen because `Timedelta` doesn't have hash equality with `datetime.timedelta`  It could easily be worked around here, but I was wondering if there's any reason not to make `Timedelta` work  like `Timestamp` and match hashes if above a certain resolution?



Discussion:

Resolving patch:
diff --git a/pandas/core/common.py b/pandas/core/common.py
--- a/pandas/core/common.py
+++ b/pandas/core/common.py
@@ -1851,9 +1851,9 @@ def _maybe_box(indexer, values, obj, key):
 def _maybe_box_datetimelike(value):
     # turn a datetime like into a Timestamp/timedelta as needed
 
-    if isinstance(value, np.datetime64):
+    if isinstance(value, (np.datetime64, datetime)):
         value = tslib.Timestamp(value)
-    elif isinstance(value, np.timedelta64):
+    elif isinstance(value, (np.timedelta64, timedelta)):
         value = tslib.Timedelta(value)
 
     return value


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -3037,6 +3037,29 @@ def create_data(constructor):
         assert_frame_equal(result_datetime, expected)
         assert_frame_equal(result_Timestamp, expected)
 
+    def test_constructor_dict_timedelta64_index(self):
+        # GH 10160
+        td_as_int = [1, 2, 3, 4]
+
+        def create_data(constructor):
+            return dict((i, {constructor(s): 2*i}) for i, s in enumerate(td_as_int))
+
+        data_timedelta64 = create_data(lambda x: np.timedelta64(x, 'D'))
+        data_timedelta = create_data(lambda x: timedelta(days=x))
+        data_Timedelta = create_data(lambda x: Timedelta(x, 'D'))
+
+        expected = DataFrame([{0: 0, 1: None, 2: None, 3: None},
+                              {0: None, 1: 2, 2: None, 3: None},
+                              {0: None, 1: None, 2: 4, 3: None},
+                              {0: None, 1: None, 2: None, 3: 6}],
+                             index=[Timedelta(td, 'D') for td in td_as_int])
+
+        result_timedelta64 = DataFrame(data_timedelta64)
+        result_timedelta = DataFrame(data_timedelta)
+        result_Timedelta = DataFrame(data_Timedelta)
+        assert_frame_equal(result_timedelta64, expected)
+        assert_frame_equal(result_timedelta, expected)
+        assert_frame_equal(result_Timedelta, expected)
 
     def _check_basic_constructor(self, empty):
         "mat: 2d matrix with shpae (3, 2) to input. empty - makes sized objects"
diff --git a/pandas/tseries/tests/test_timedeltas.py b/pandas/tseries/tests/test_timedeltas.py
--- a/pandas/tseries/tests/test_timedeltas.py
+++ b/pandas/tseries/tests/test_timedeltas.py
@@ -885,6 +885,24 @@ def test_pickle(self):
         v_p = self.round_trip_pickle(v)
         self.assertEqual(v,v_p)
 
+    def test_timedelta_hash_equality(self):
+        #GH 11129
+        v = Timedelta(1, 'D')
+        td = timedelta(days=1)
+        self.assertEqual(hash(v), hash(td))
+
+        d = {td: 2}
+        self.assertEqual(d[v], 2)
+
+        tds = timedelta_range('1 second', periods=20)
+        self.assertTrue(
+            all(hash(td) == hash(td.to_pytimedelta()) for td in tds))
+
+        # python timedeltas drop ns resolution
+        ns_td = Timedelta(1, 'ns')
+        self.assertNotEqual(hash(ns_td), hash(ns_td.to_pytimedelta()))
+
+
 class TestTimedeltaIndex(tm.TestCase):
     _multiprocess_can_split_ = True