BUG: DataFrame constructor fails to repeat length-1 list to length of index if it contains a Timestamp/datetime64 #42810
Labels
Constructors
Series/DataFrame/Index/pd.array Constructors
Datetime
Datetime data dtype
Regression
Functionality that used to work in a prior pandas version
Milestone
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
This will throw an exception:
The same exception will also be raised when the
data
argument is a list-of-dicts but not whendata
is a dict-of-lists:Problem description
From what I can tell, the
DataFrame
constructor supports supplying a length-1 list and an index of length greater than 1 as long as the list doesn't contain a value of typeTimestamp
/datetime64
. Raising an error only for these types is therefore inconsistent and could lead to user confusion. It is also a change from the behaviour in pandas 1.2 where it was possible to supply a length-1 list containing aTimestamp
.In Pandas 1.2.5:
Expected Output
The length-1 list should be repeated to the length of the index:
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : c7f7443
python : 3.9.4.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
Version : Darwin Kernel Version 18.7.0: Mon May 3 20:41:19 PDT 2021; root:xnu-4903.278.68~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8
pandas : 1.3.1
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 20.2.3
setuptools : 49.2.1
Cython : None
pytest : None
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 : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
Notes
This appear to be caused by the code going through the
elif isinstance(data, ABCExtensionArray)
branch ofsanitize_array
which causes it to return early before_sanitize_ndim
is called. Removing that early return appears to fix the problem and it didn't cause any tests to fail when I ran./test_fast.sh
. If this change makes sense I'd be happy to put up a PR.The text was updated successfully, but these errors were encountered: