1

I have a numpy array of Pandas Timestamps:

array([[Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
        Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
        Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T')],
       [Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
        Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
        Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T')],
       [Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
        Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
        Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T')]], dtype=object)

I cannot create a DataFrame from this array, as attempting to do so throws the following error:

AssertionError: Number of Block dimensions (1) must equal number of axes (2)

You can see that the array is clearly 2 dimensional, which i verified by using ndim.

Why can't I create a DataFrame?

1 Answer 1

1

I think you can use list comprehension:

import pandas as pd
import numpy as np

a =np.array([[pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
        pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
        pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T')],
       [pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
        pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
        pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T')],
       [pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
        pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
        pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T')]], dtype=object)

df = pd.DataFrame([x for x in a], columns=['a','b','c'])
print (df)
                          a                         b  \
0 2016-05-02 15:50:00+00:00 2016-05-02 15:50:00+00:00   
1 2016-05-02 17:10:00+00:00 2016-05-02 17:10:00+00:00   
2 2016-05-02 20:25:00+00:00 2016-05-02 20:25:00+00:00   

                          c  
0 2016-05-02 15:50:00+00:00  
1 2016-05-02 17:10:00+00:00  
2 2016-05-02 20:25:00+00:00  

Another solution is DataFrame.from_records:

print (pd.DataFrame.from_records(a, columns=['a','b','c']))
                          a                         b  \
0 2016-05-02 15:50:00+00:00 2016-05-02 15:50:00+00:00   
1 2016-05-02 17:10:00+00:00 2016-05-02 17:10:00+00:00   
2 2016-05-02 20:25:00+00:00 2016-05-02 20:25:00+00:00   

                          c  
0 2016-05-02 15:50:00+00:00  
1 2016-05-02 17:10:00+00:00  
2 2016-05-02 20:25:00+00:00  

See alternate constructors of df.

Sign up to request clarification or add additional context in comments.

1 Comment

This definitely answers my question, so thank you. I was asking because the original problem I had was that I was attempting to transpose a DataFrame of Timestamps. Even when constructing a DataFrame with from_records, transposing throws the same AssertionError as before. For now, I am transposing the numpy array before I construct the DataFrame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.