You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Converting a sequence of strings, ``datetime`` objects, or ``np.datetime64`` objects to
133
+
a ``datetime64`` dtype now performs inference on the appropriate resolution (AKA unit) for the output dtype. This affects :class:`Series`, :class:`DataFrame`, :class:`Index`, :class:`DatetimeIndex`, and :func:`to_datetime`.
134
+
135
+
Previously, these would always give nanosecond resolution:
136
+
137
+
.. code-block:: ipython
138
+
139
+
In [1]: dt = pd.Timestamp("2024-03-22 11:36").to_pydatetime()
140
+
In [2]: pd.to_datetime([dt]).dtype
141
+
Out[2]: dtype('<M8[ns]')
142
+
In [3]: pd.Index([dt]).dtype
143
+
Out[3]: dtype('<M8[ns]')
144
+
In [4]: pd.DatetimeIndex([dt]).dtype
145
+
Out[4]: dtype('<M8[ns]')
146
+
In [5]: pd.Series([dt]).dtype
147
+
Out[5]: dtype('<M8[ns]')
148
+
149
+
This now infers the unit microsecond unit "us" from the pydatetime object, matching the scalar :class:`Timestamp` behavior.
150
+
151
+
.. ipython:: python
152
+
153
+
In [1]: dt = pd.Timestamp("2024-03-22 11:36").to_pydatetime()
154
+
In [2]: pd.to_datetime([dt]).dtype
155
+
In [3]: pd.Index([dt]).dtype
156
+
In [4]: pd.DatetimeIndex([dt]).dtype
157
+
In [5]: pd.Series([dt]).dtype
158
+
159
+
Similar when passed a sequence of ``np.datetime64`` objects, the resolution of the passed objects will be retained (or for lower-than-second resolution, second resolution will be used).
160
+
161
+
When passing strings, the resolution will depend on the precision of the string, again matching the :class:`Timestamp` behavior. Previously:
162
+
163
+
.. code-block:: ipython
164
+
165
+
In [2]: pd.to_datetime(["2024-03-22 11:43:01"]).dtype
166
+
Out[2]: dtype('<M8[ns]')
167
+
In [3]: pd.to_datetime(["2024-03-22 11:43:01.002"]).dtype
168
+
Out[3]: dtype('<M8[ns]')
169
+
In [4]: pd.to_datetime(["2024-03-22 11:43:01.002003"]).dtype
170
+
Out[4]: dtype('<M8[ns]')
171
+
In [5]: pd.to_datetime(["2024-03-22 11:43:01.002003004"]).dtype
172
+
Out[5]: dtype('<M8[ns]')
173
+
174
+
The inferred resolution now matches that of the input strings:
175
+
176
+
.. ipython:: python
177
+
178
+
In [2]: pd.to_datetime(["2024-03-22 11:43:01"]).dtype
179
+
In [3]: pd.to_datetime(["2024-03-22 11:43:01.002"]).dtype
180
+
In [4]: pd.to_datetime(["2024-03-22 11:43:01.002003"]).dtype
181
+
In [5]: pd.to_datetime(["2024-03-22 11:43:01.002003004"]).dtype
182
+
183
+
In cases with mixed-resolution inputs, the highest resolution is used:
184
+
185
+
.. code-block:: ipython
186
+
187
+
In [2]: pd.to_datetime([pd.Timestamp("2024-03-22 11:43:01"), "2024-03-22 11:43:01.002"]).dtype
0 commit comments