2015-09-23 2 views
1

J'ai un fichier HDF5 contenant des tables pandas Series/DataFrame. Je dois obtenir (pandas géants) index d'une table stockée sous une clé HDF, mais pas nécessairement toute la table:Le moyen le plus efficace d'obtenir l'index d'une table à partir de HDF5

Je peux penser à deux (en fait les mêmes) méthodes d'obtenir l'indice:

import pandas as pd 

hdfPath = 'c:/example.h5' 
hdfKey = 'dfkey' 
# way 1: 
with pd.HDFStore(hdfPath) as hdf: 
    index = hdf[hdfKey].index 

# way 2: 
index = pd.read_hdf(hdfPath, hdfKey) 

Cependant, pour une série de pandas géants de ~ 2000 lignes cela prend 0,6 sec:

%timeit pd.read_hdf(hdfPath, hdfKey).index 
1 loops, best of 3: 605 ms per loop 

y at-il un moyen d'obtenir que l'index d'une table HDF?

Répondre

0

L'objet HDFStore possède une méthode select_column qui vous permettra d'obtenir l'index. Notez qu'il retournera une série avec l'index comme valeurs.

with pd.HDFStore(hdfPath) as hdf: 
    index = hdf.select_column(hdfKey, 'index').values