python - Adding or remvoing specific rows or columns in an h5py dataset -


once create h5py dataset, how add or remove specific rows or columns nxm array?

my question similar this one, don't want blindly truncate or expand array. when removing, need able specify exact row or column remove.

for adding, know have specify maxshape=(none, none) when creating initial dataset, resize method doesn't seem let specify rows or columns truncated if shrink size.

h5py isn't designed doing this. pandas might better library use, it's built around concept of tables.

having said that, here's how it:

in [1]: f = h5py.file('test.h5')  in [2]: arr = rand(4,4)  in [3]: dset = f.create_dataset('foo',data=arr,maxshape=(2000,2000))  in [4]: dset[:] out[4]: array([[ 0.29732874,  0.59310285,  0.61116263,  0.79950116],        [ 0.4194363 ,  0.4691813 ,  0.95648712,  0.56120731],        [ 0.76868585,  0.07556214,  0.39854704,  0.73415885],        [ 0.0919063 ,  0.0420656 ,  0.35082375,  0.62565894]])  in [5]: dset[1:-1,:] = dset[2:,:]  in [6]: dset.resize((3,4))  in [7]: dset[:] out[7]: array([[ 0.29732874,  0.59310285,  0.61116263,  0.79950116],        [ 0.76868585,  0.07556214,  0.39854704,  0.73415885],        [ 0.0919063 ,  0.0420656 ,  0.35082375,  0.62565894]]) 

this removes column 1 dset. assigning columns 2 , 3 1 , 2, respectively, before shrinking dataset 1 column. swap subscripts remove row 1. can write wrapper around if you're going doing lot.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -