Skip to content

BUG: pd.DataFrame is created inconsistently from numpy.ndarray for non-native pandas dtypes such as: datetime64[ms], etc.. #39422

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

Closed
2 of 3 tasks
mheydel opened this issue Jan 26, 2021 · 2 comments · Fixed by #39442
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure
Milestone

Comments

@mheydel
Copy link

mheydel commented Jan 26, 2021

  • 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

@mheydel mheydel added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 26, 2021
@phofl phofl added Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2021
@jreback jreback added this to the 1.3 milestone Jan 28, 2021
@jorisvandenbossche
Copy link
Member

This was actually a regression (compared to 1.0.5, but still worth including in the bug fix release IMO since it's silently giving garbled results)

@jorisvandenbossche
Copy link
Member

Backported in #39475

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure
Projects
None yet
4 participants