Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
In [2]: import pandas as pd
In [3]: from io import StringIO
In [4]: csv_str = ',t1,t2\n0,2020-08-01 09:00:00,1940-08-31 06:00:00\n1,1920-05-01 10:30:00,2020-08-02 10:00:00\n'
In [5]: df = pd.read_csv(StringIO(csv_str))
In [6]: df = df[['t1', 't2']]
In [7]: df['t1'] = df['t1'].astype('datetime64[us]')
In [8]: df['t2'] = df['t2'].astype('datetime64[us]')
In [9]: df
Out[9]:
t1 t2
0 2020-08-01 09:00:00 1940-08-31 06:00:00
1 1920-05-01 10:30:00 2020-08-02 10:00:00
In [10]: df.max(axis=1)
Out[10]:
0 2020-08-01 09:00:00
1 2020-08-02 10:00:00
dtype: datetime64[us]
In [11]: df['t2'] = df['t2'].astype('datetime64[ms]')
In [12]: df.dtypes
Out[12]:
t1 datetime64[us]
t2 datetime64[ms]
dtype: object
In [13]: df.max(axis=1)
Out[13]:
0 2020-08-01 09:00:00
1 2020-08-02 10:00:00
dtype: datetime64[ns]
Issue Description
In the above example, a row-wise operation on different time resolutions seems to be defaulting to ns
time resolution, whereas here we can clearly choose to return datetime64[us]
(the lowest possible time resolution depending on the operation performed-min
).
Expected Behavior
In [13]: df.max(axis=1)
Out[13]:
0 2020-08-01 09:00:00
1 2020-08-02 10:00:00
dtype: datetime64[us]
Installed Versions
Comment From: phofl
cc @jbrockmendel This looks like it is cast to ns intentionally for now if we have more than one unit
Comment From: jbrockmendel
Looks like its the transpose that converts to ns. I don't think this is intentional.
Comment From: phofl
The cast happens in https://github.com/pandas-dev/pandas/blob/e9e034badbd2e36379958b93b611bf666c7ab360/pandas/core/dtypes/cast.py#L1410
which looked intentional for now. Do we have a find_common_resolution
helper?
Comment From: jbrockmendel
Do we have a find_common_resolution helper?
No, but the numpy dtypes are comparable
>>> dtypes = [np.dtype(f"M8[{unit}]") for unit in ["s", "ms", "us"]]
>>> min(dtypes)
dtype('<M8[s]')
>>> max(dtypes)
dtype('<M8[us]')