Evan Rosica
Evan Rosica

Reputation: 1242

Creating a MultiIndex by Zipping DatetimeIndex is Causing Error

I'm trying to build a multiIndex for a pandas DataFrame which is to store time series data for several individuals.

I thought a good way to do this would be as follows:

D1 = pd.date_range(start='1/1/2018', periods=2, freq='H')
D2 = pd.date_range(start='3/4/2018', periods=3, freq='H')
l1=[1,2]       # the individuals' numbers
l2 = [D1,D2]
l = list(zip(l1,l2))
M = pd.MultiIndex.from_tuples(l)

and the desired output would be a multiIndex of the form below:

1    2018-01-01 00:00:00
     2018-01-01 01:00:00
2    2018-03-04 00:00:00
     2018-03-04 01:00:00
     2018-03-04 02:00:00

However, I'm getting TypeError: unhashable type: 'DatetimeIndex'. Any help would be appreciated.

Upvotes: 1

Views: 38

Answers (1)

jezrael
jezrael

Reputation: 863451

Solution is flatten second zipped values of l2 for list of tuples:

l = [(a,x) for a, b in zip(l1,l2) for x in b]
print(l)
[(1, Timestamp('2018-01-01 00:00:00', freq='H')), 
 (1, Timestamp('2018-01-01 01:00:00', freq='H')), 
 (2, Timestamp('2018-03-04 00:00:00', freq='H')), 
 (2, Timestamp('2018-03-04 01:00:00', freq='H')), 
 (2, Timestamp('2018-03-04 02:00:00', freq='H'))]

M = pd.MultiIndex.from_tuples(l)

print(M)
MultiIndex(levels=[[1, 2], [2018-01-01 00:00:00, 2018-01-01 01:00:00, 
                            2018-03-04 00:00:00, 2018-03-04 01:00:00, 
                            2018-03-04 02:00:00]],
           codes=[[0, 0, 1, 1, 1], [0, 1, 2, 3, 4]])

s = pd.Series(range(5), index=M)
print (s)
1  2018-01-01 00:00:00    0
   2018-01-01 01:00:00    1
2  2018-03-04 00:00:00    2
   2018-03-04 01:00:00    3
   2018-03-04 02:00:00    4
dtype: int64

Upvotes: 2

Related Questions