-
-
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
pd.Concat with empty series drops series.name and series.index.name #24685
Comments
pls try with 0.23.4 , master if you can |
with pandas 0.23.4 -> same result Updated problem description |
I can also reproduce on master
|
Looks like DataFrame index name is lost too after concatenating it with empty dataframe: import pandas as pd
df = pd.DataFrame([['a', 5]], columns=['whatever', 'id']).set_index('id')
empty_df = pd.DataFrame(columns=['type']) #note: at least one column in empty DF is required
result = pd.concat([empty_df, df], sort=False)
print(result)
# type whatever
#5 NaN a Everything works as expected if empty DataFrame has no columns: import pandas as pd
df = pd.DataFrame([['a', 5]], columns=['whatever', 'id']).set_index('id')
empty_df = pd.DataFrame(columns=[]) # no columns here
result = pd.concat([empty_df, df], sort=False)
print(result)
# whatever
#id
#5 a Installed versions
|
Probably the same issue in another context: Series.name is lost, when the Series object is put into a DataFrame. Even if the Series.name is set after it has been put into the DataFrame, it is lost as soon as any manipulation (even with a different columns) on the DataFrame occurs. In contrast to Series.name, Index.name is preserved. Sample codeimport pandas as pd
idx = pd.Index(['x', 'y', 'z'], name='my_index')
a = pd.Series([1,2,3], name='series_a', index=idx)
b = pd.Series([4,5,6], name='series_b', index=idx)
df = pd.DataFrame({'a': a})
assert idx.name == df.index.name # succeeds
assert a.index.name == df.a.index.name # succeeds
df.a.name = a.name # shouldn't be neccesary, but obviously is
assert a.name == df.a.name # fails if the previous line is not present
df['b'] = b
assert idx.name == df.index.name # succeeds
assert b.index.name == df.b.index.name # succeeds
df.a.name = a.name # shouldn't be neccesary, but obviously is
assert a.name == df.a.name # fails if the previous line is not present
df['b'] *= 2
assert idx.name == df.index.name # succeeds
assert b.index.name == df.b.index.name # succeeds
assert a.name == df.a.name # fails
INSTALLED VERSIONS
------------------
commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-42-lowlatency
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8
pandas : 1.0.2 |
In #11082 this was declared a bug. Should we close this issue or has that changed? |
#11082 was fully patched
looks ok to me so would review the postings here, but I think they might be fixed (maybe needs a test) |
The Poster proposed that the result would keep the name of the non empty Series, which is exactly the opposite #11082, that is the reason why I am asking. We have already tests checking that the Name is dropped |
Since a test exists that the name is dropped, looks like we can close this issue |
Concat with empty Series
Using the following code:
Problem description
The code pasted intend to probe the what if an empty series is concated with a series fully defined
To get the expected result:
Same series with name expected name and index.
at pandas 0.17 it works as expected but when it ran at pandas 0.18 and so on it drops expected name and index.
it only works that empty series with forced name and index as the following snapshot shows.
Same result with pandas 0.23.4
The text was updated successfully, but these errors were encountered: