Why concatenation can’t handle Nones in categorical columns when the DF can hold it in the first place

I have 2 DFs with object type columns, which work fine with concatenation.

Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', None]})
df2 = pd.DataFrame({'A': ['A4', 'A5'], 'B': [None, None]})
print(">>>>>>>>>>>>>>> Original DFs")
print(df1)
print(df2)
print(">>>>>>>>>>>>>>> Original DTypes")
print(df1.dtypes)
print(df2.dtypes)
</code>
<code>df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', None]}) df2 = pd.DataFrame({'A': ['A4', 'A5'], 'B': [None, None]}) print(">>>>>>>>>>>>>>> Original DFs") print(df1) print(df2) print(">>>>>>>>>>>>>>> Original DTypes") print(df1.dtypes) print(df2.dtypes) </code>
df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', None]})
df2 = pd.DataFrame({'A': ['A4', 'A5'], 'B': [None, None]})

print(">>>>>>>>>>>>>>> Original DFs")
print(df1)
print(df2)

print(">>>>>>>>>>>>>>> Original DTypes")
print(df1.dtypes)
print(df2.dtypes)

Corresponding Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>>>>>>>>>>>>>>>> Original DFs
A B
0 A0 B0
1 A1 None
A B
0 A4 None
1 A5 None
>>>>>>>>>>>>>>> Original DTypes
A object
B object
dtype: object
A object
B object
dtype: object
>>>>>>>>>>>>>>> Concatenation 1 - No Warning
A B
0 A0 B0
1 A1 None
0 A4 None
1 A5 None
</code>
<code>>>>>>>>>>>>>>>> Original DFs A B 0 A0 B0 1 A1 None A B 0 A4 None 1 A5 None >>>>>>>>>>>>>>> Original DTypes A object B object dtype: object A object B object dtype: object >>>>>>>>>>>>>>> Concatenation 1 - No Warning A B 0 A0 B0 1 A1 None 0 A4 None 1 A5 None </code>
>>>>>>>>>>>>>>> Original DFs
    A     B
0  A0    B0
1  A1  None
    A     B
0  A4  None
1  A5  None
>>>>>>>>>>>>>>> Original DTypes
A    object
B    object
dtype: object
A    object
B    object
dtype: object
>>>>>>>>>>>>>>> Concatenation 1 - No Warning
    A     B
0  A0    B0
1  A1  None
0  A4  None
1  A5  None

But if I do the same with categorical columns, I get a FutureWarning

Code with Categorical data type

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>print(">>>>>>>>>>>>>>> Categorical DTypes")
df1 = df1.astype('category')
df2 = df2.astype('category')
print(df1.dtypes)
print(df2.dtypes)
print(">>>>>>>>>>>>>>> Concatenation 2 - Gives warning")
print(pd.concat([df1, df2]))
</code>
<code>print(">>>>>>>>>>>>>>> Categorical DTypes") df1 = df1.astype('category') df2 = df2.astype('category') print(df1.dtypes) print(df2.dtypes) print(">>>>>>>>>>>>>>> Concatenation 2 - Gives warning") print(pd.concat([df1, df2])) </code>
print(">>>>>>>>>>>>>>> Categorical DTypes")
df1 = df1.astype('category')
df2 = df2.astype('category')
print(df1.dtypes)
print(df2.dtypes)

print(">>>>>>>>>>>>>>> Concatenation 2 - Gives warning")
print(pd.concat([df1, df2]))

Corresponding Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>>>>>>>>>>>>>>>> Categorical DTypes
A category
B category
dtype: object
A category
B category
dtype: object
>>>>>>>>>>>>>>> Concatenation 2 - Gives warning
bla.py:37: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.
print(pd.concat([df1, df2]))
A B
0 A0 B0
1 A1 NaN
0 A4 NaN
1 A5 NaN
</code>
<code>>>>>>>>>>>>>>>> Categorical DTypes A category B category dtype: object A category B category dtype: object >>>>>>>>>>>>>>> Concatenation 2 - Gives warning bla.py:37: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation. print(pd.concat([df1, df2])) A B 0 A0 B0 1 A1 NaN 0 A4 NaN 1 A5 NaN </code>
>>>>>>>>>>>>>>> Categorical DTypes
A    category
B    category
dtype: object
A    category
B    category
dtype: object
>>>>>>>>>>>>>>> Concatenation 2 - Gives warning
bla.py:37: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.
  print(pd.concat([df1, df2]))
    A    B
0  A0   B0
1  A1  NaN
0  A4  NaN
1  A5  NaN

df2 had a NaN to start with and there is no problem with it, but when I try to concatenate with all NaN columns, I get the warning. The suggestion is to remove such entries altogether. Why is this the case? Why does concatenation seem to have issues with NaNs?

Here is the full code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
def bla():
'''The main function, that can also be called fromother scripts as an API'''
df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', None]})
df2 = pd.DataFrame({'A': ['A4', 'A5'], 'B': [None, None]})
print(">>>>>>>>>>>>>>> Original DFs")
print(df1)
print(df2)
print(">>>>>>>>>>>>>>> Original DTypes")
print(df1.dtypes)
print(df2.dtypes)
print(">>>>>>>>>>>>>>> Concatenation 1 - No Warning")
print(pd.concat([df1, df2]))
print(">>>>>>>>>>>>>>> Categorical DTypes")
df1 = df1.astype('category')
df2 = df2.astype('category')
print(df1.dtypes)
print(df2.dtypes)
print(">>>>>>>>>>>>>>> Concatenation 2 - Gives warning")
print(pd.concat([df1, df2]))
if __name__ == '__main__':
bla()
</code>
<code>import pandas as pd def bla(): '''The main function, that can also be called fromother scripts as an API''' df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', None]}) df2 = pd.DataFrame({'A': ['A4', 'A5'], 'B': [None, None]}) print(">>>>>>>>>>>>>>> Original DFs") print(df1) print(df2) print(">>>>>>>>>>>>>>> Original DTypes") print(df1.dtypes) print(df2.dtypes) print(">>>>>>>>>>>>>>> Concatenation 1 - No Warning") print(pd.concat([df1, df2])) print(">>>>>>>>>>>>>>> Categorical DTypes") df1 = df1.astype('category') df2 = df2.astype('category') print(df1.dtypes) print(df2.dtypes) print(">>>>>>>>>>>>>>> Concatenation 2 - Gives warning") print(pd.concat([df1, df2])) if __name__ == '__main__': bla() </code>
import pandas as pd


def bla():
    '''The main function, that can also be called fromother scripts as an API'''

    df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', None]})
    df2 = pd.DataFrame({'A': ['A4', 'A5'], 'B': [None, None]})

    print(">>>>>>>>>>>>>>> Original DFs")
    print(df1)
    print(df2)

    print(">>>>>>>>>>>>>>> Original DTypes")
    print(df1.dtypes)
    print(df2.dtypes)

    print(">>>>>>>>>>>>>>> Concatenation 1 - No Warning")
    print(pd.concat([df1, df2]))

    print(">>>>>>>>>>>>>>> Categorical DTypes")
    df1 = df1.astype('category')
    df2 = df2.astype('category')
    print(df1.dtypes)
    print(df2.dtypes)

    print(">>>>>>>>>>>>>>> Concatenation 2 - Gives warning")
    print(pd.concat([df1, df2]))


if __name__ == '__main__':
    bla()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật