简体   繁体   中英

Squeeze of pandas dataframe with 1-observation (pandas 0.18)

This could be considered an edge case, but I am finding an inconsistency when trying to squeeze (reduce to time series) a pandas dataframe consisting of one observation only.

import pandas as pd

xx = pd.DataFrame(1, columns = ['A'], index = ['first_index'])

xx.squeeze() 
#1 (float)

pd.Series(xx)
#Out[4]: 
#0    1
#dtype: object

Expected output:

pd.Series(xx, index = xx.index)
Out[5]: 
first_index    1
dtype: object

My question is: why a DataFrame with only one observation is treated as 1-d object by the squeeze function?

Is this a bug or am I missing any design reason for this?

This introduces a bit of overhead but it may work:

pd.concat([xx,xx]).squeeze().iloc[:len(xx)]
Out[1778]: 
first_index    1
Name: A, dtype: int64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM