BUG: pd.DataFrame is created inconsistently from numpy.ndarray for non-native pandas dtypes such as: datetime64[ms], etc.. · Issue #39422 · pandas-dev/pandas (original) (raw)
- 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.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
import pandas as pd import numpy as np
a = np.array( [ ["2015-01-01", "2015-01-02", "2015-01-03"], ["2017-01-01", "2017-01-02", "2017-02-03"], ], dtype="datetime64[ms]", )
print(pd.DataFrame(a)) 0 1 2 0 2015-01-01 2015-01-03 2017-01-02 1 2015-01-02 2017-01-01 2017-02-03
b = np.array( [ ["2015-01-01", "2015-01-02", "2015-01-03"], ["2017-01-01", "2017-01-02", "2017-02-03"], ], dtype="datetime64[M]", )
print(pd.DataFrame(b)) 0 1 2 0 2015-01-01 2015-01-03 2017-01-02 1 2015-01-02 2017-01-01 2017-02-03
c = np.array( [ ["2015-01-01", "2015-01-02", "2015-01-03"], ["2017-01-01", "2017-01-02", "2017-02-03"], ], dtype="datetime64[ns]", )
print(pd.DataFrame(c)) 0 1 2 0 2015-01-01 2015-01-02 2015-01-03 1 2017-01-01 2017-01-02 2017-02-03
Problem description
The exists an inconsistency how pd.DataFrame() is created from 2D numpy.ndarray for not pandas native type. The dataframe should be transposed for datetime64[M], datetime64[ms] etc.
Expected Output
print(pd.DataFrame(a)) 0 1 2 0 2015-01-01 2015-01-02 2015-01-03 1 2017-01-01 2017-01-02 2017-02-03
print(pd.DataFrame(b)) 0 2015-01-01 2015-01-02 2015-01-03 1 2017-01-01 2017-01-02 2017-02-03
Possible Hint
d = np.array( [ ["2015-01-01", "2015-01-02", "2015-01-03"], ["2017-01-01", "2017-01-02", "2017-02-03"], ], dtype="datetime64[ms]", order="f", )
print(pd.DataFrame(d)) 0 2015-01-01 2015-01-02 2015-01-03 1 2017-01-01 2017-01-02 2017-02-03
Output of pd.show_versions()
INSTALLED VERSIONS
commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 1.1.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 21.0
setuptools : 49.6.0.post20210108
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.22
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None