From 4037aa896f8993a2dcae7997bde071faf5498b1b Mon Sep 17 00:00:00 2001 From: Sergey Volkov Date: Thu, 1 Aug 2024 17:22:24 +0200 Subject: [PATCH] make dtype to be ignored in constructors if data is Series or DataFrame The note in the docs wasn't sufficient: the dtype kwarg was ignored in certain cases. Set it to None if creating Series from another Series and the same for DataFrames. See GH pandas-dev#59060. --- pandas/core/frame.py | 1 + pandas/core/series.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ea91046f4b8e4..104557dd337fd 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -705,6 +705,7 @@ def __init__( if isinstance(data, DataFrame): data = data._mgr allow_mgr = True + dtype = None if not copy: # if not copying data, ensure to still return a shallow copy # to avoid the result sharing the same Manager diff --git a/pandas/core/series.py b/pandas/core/series.py index a197886748bce..7f59989701656 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -445,6 +445,7 @@ def __init__( "compound dtype. Use DataFrame instead." ) elif isinstance(data, Series): + dtype = None if index is None: index = data.index data = data._mgr.copy(deep=False)