-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: erroneous initialization of a DataFrame with Series objects #42818
Comments
use a list of the Series what u r passing is actually valid but thr 2nd arg is the index |
@jreback thanks for responding! I don't fully get what you mean "it is actually valid"... the result is really not what I've expected let me clarify this a bit more: x = [1,2,3,4,5]
pd.DataFrame(x, index=x) # OK
pd.DataFrame(np.array(x), index=x) # OK
pd.Series(x).to_frame().set_index(pd.Series(x)) # OK
pd.DataFrame(pd.Series(x), index=x) # NOT OK
pd.DataFrame(dict(a=pd.Series(x)), index=x) # NOT OK
pd.DataFrame([pd.Series(x)], index=x) # NOT OK (generates a 2D grid of values) |
The index of the series and the DataFrame Index are aligned, this causes the nans, since you do not have matching entries in there.
returns
which should make clear what happens here |
@phofl so basically if the data (e.g. I consider myself as a long-term user of pandas but this was so far not clear to me!
not sure, but how about a comment in the doc of >>> pd.DataFrame(dict(a=[1,2,3,4,5],
>>> b=pd.Series([3,4,5], index=[3,4,5])),
>>> index=[1,2,3,4,5])
a b
1 1 NaN
2 2 NaN
3 3 3.0
4 4 4.0
5 5 5.0 |
PRs always welcome to clarify/enhance the documentation. |
Hi, can I pick this issue as my first OSS contribution? |
@tyuyoshi sure. go for it! |
Can I fix the issues? |
Hi, I would like to take up this problem as my first open source contribution. |
Hello, Can I use this issue as my first contribution? |
take |
take |
df = pd.DataFrame(prcpSeries, columns=['prcp']) df1 = pd.DataFrame(prcpSeries, columns=['prcp']) df = pd.concat([df1, df2, ...], join='outer', axis=1) In [21]: dfA = pd.DataFrame([1,2], columns=['A']) In [22]: dfB = pd.DataFrame([1], columns=['B']) In [23]: pd.concat([dfA, dfB], join='outer', axis=1) |
Sorry, but I have already open PR #42960 |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Problem description
I would expect
pd.Series
objects to be valid inputs for theDataFrame
constructor.If this is not the case a warning (or even raising an error) would be nice...
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : c7f7443
python : 3.9.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : AMD64 Family 25 Model 33 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : en
LOCALE : German_Austria.1252
pandas : 1.3.1
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.1
setuptools : 49.6.0.post20210108
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.0
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: