user3235542
user3235542

Reputation:

putting numpy ndarray data into pandas

I want to put the following data into pandas for further analysis.

import numpy as np
import pandas as pd
from pandas import DataFrame

data = np.array([[[1, 1, 1, np.nan, 1], [np.nan, 1, 1, 1, 1]],
                 [[2, np.nan, 2, 2, 2], [2, np.nan, 2, 2, 2]],
                 [[3, 3, 3, np.nan, 3], [3, 3, 3, 3, np.nan]]])

pnda = pd.Series(data)

print pnda

But the following error occurs:

Exception: Data must be 1-dimensional

What is the good way of doing it? My further analysis is to filling the np.nan values by interpolation with cubic or polynomial method and output the result as numpy array.

Upvotes: 5

Views: 4758

Answers (2)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59005

Based on your comments you can achieve what you want if you reshape data, interpolate using the DataFrame.interpolate() method and then return the array to its original value. It works for pandas 0.13.1.

df = pd.DataFrame(data.reshape(2, -1))
df.interpolate(axis=1).values.reshape(data.shape)
#array([[[1, 1, 1, 1, 1],
#        [1, 1, 1, 1, 1]],
#
#       [[2, 2, 2, 2, 2],
#        [2, 2, 2, 2, 2]],
#
#       [[3, 3, 3, 3, 3],
#        [3, 3, 3, 3, 3]]], dtype=int64)

Upvotes: 2

Paul H
Paul H

Reputation: 68216

Try using a panel:

import numpy as np
import pandas as pd

data = np.array([[[1, 1, 1, np.nan, 1], [np.nan, 1, 1, 1, 1]],
                 [[2, np.nan, 2, 2, 2], [2, np.nan, 2, 2, 2]],
                 [[3, 3, 3, np.nan, 3], [3, 3, 3, 3, np.nan]]])

x = pd.Panel(data)
x

<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 2 (major_axis) x 5 (minor_axis)
Items axis: 0 to 2
Major_axis axis: 0 to 1
Minor_axis axis: 0 to 4

And...

print(x.loc[0])
    0  1  2   3  4
0   1  1  1 NaN  1
1 NaN  1  1   1  1

Upvotes: 3

Related Questions