Skip to content

Commit 8eab6e9

Browse files
committed
undo unnecessary adjustments
1 parent 0443b2c commit 8eab6e9

File tree

8 files changed

+356
-127
lines changed

8 files changed

+356
-127
lines changed

doc/source/user_guide/timedeltas.rst

+2
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ Similarly to other of the datetime-like indices, ``DatetimeIndex`` and ``PeriodI
424424
Selections work similarly, with coercion on string-likes and slices:
425425

426426
.. ipython:: python
427+
:okwarning:
427428
428429
s["1 day":"2 day"]
429430
s["1 day 01:00:00"]
@@ -432,6 +433,7 @@ Selections work similarly, with coercion on string-likes and slices:
432433
Furthermore you can use partial string selection and the range will be inferred:
433434

434435
.. ipython:: python
436+
:okwarning:
435437
436438
s["1 day":"1 day 5 hours"]
437439

pandas/_libs/tslibs/parsing.pyx

+36-8
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ cdef inline bint does_string_look_like_time(str parse_string):
263263

264264
return 0 <= hour <= 23 and 0 <= minute <= 59
265265

266+
from pandas.util._exceptions import find_stack_level
267+
268+
269+
def du_parse_with_warning(*args, **kwargs):
270+
parsed = du_parse(*args, **kwargs)
271+
warnings.warn(
272+
"Parsing datetime strings without a format specified, "
273+
"please specify a format to avoid unexpected results",
274+
stacklevel=find_stack_level(),
275+
)
276+
266277

267278
def parse_datetime_string(
268279
# NB: This will break with np.str_ (GH#32264) even though
@@ -290,8 +301,12 @@ def parse_datetime_string(
290301

291302
if does_string_look_like_time(date_string):
292303
# use current datetime as default, not pass _DEFAULT_DATETIME
293-
dt = du_parse(date_string, dayfirst=dayfirst,
294-
yearfirst=yearfirst, **kwargs)
304+
dt = du_parse_with_warning(
305+
date_string,
306+
dayfirst=dayfirst,
307+
yearfirst=yearfirst,
308+
**kwargs,
309+
)
295310
return dt
296311

297312
dt, _ = _parse_delimited_date(date_string, dayfirst)
@@ -307,8 +322,13 @@ def parse_datetime_string(
307322
pass
308323

309324
try:
310-
dt = du_parse(date_string, default=_DEFAULT_DATETIME,
311-
dayfirst=dayfirst, yearfirst=yearfirst, **kwargs)
325+
dt = du_parse_with_warning(
326+
date_string,
327+
default=_DEFAULT_DATETIME,
328+
dayfirst=dayfirst,
329+
yearfirst=yearfirst,
330+
**kwargs,
331+
)
312332
except TypeError:
313333
# following may be raised from dateutil
314334
# TypeError: 'NoneType' object is not iterable
@@ -706,7 +726,11 @@ def try_parse_dates(
706726
date = datetime.now()
707727
default = datetime(date.year, date.month, 1)
708728

709-
parse_date = lambda x: du_parse(x, dayfirst=dayfirst, default=default)
729+
parse_date = lambda x: du_parse_with_warning(
730+
x,
731+
dayfirst=dayfirst,
732+
default=default,
733+
)
710734

711735
# EAFP here
712736
try:
@@ -753,13 +777,17 @@ def try_parse_date_and_time(
753777
date = datetime.now()
754778
default = datetime(date.year, date.month, 1)
755779

756-
parse_date = lambda x: du_parse(x, dayfirst=dayfirst, default=default)
780+
parse_date = lambda x: du_parse_with_warning(
781+
x,
782+
dayfirst=dayfirst,
783+
default=default,
784+
)
757785

758786
else:
759787
parse_date = date_parser
760788

761789
if time_parser is None:
762-
parse_time = lambda x: du_parse(x)
790+
parse_time = lambda x: du_parse_with_warning(x)
763791

764792
else:
765793
parse_time = time_parser
@@ -980,7 +1008,7 @@ def guess_datetime_format(dt_str, bint dayfirst=False):
9801008
datetime_attrs_to_format.insert(0, day_attribute_and_format)
9811009

9821010
try:
983-
parsed_datetime = du_parse(dt_str, dayfirst=dayfirst)
1011+
parsed_datetime = du_parse_with_warning(dt_str, dayfirst=dayfirst)
9841012
except (ValueError, OverflowError):
9851013
# In case the datetime can't be parsed, its format cannot be guessed
9861014
return None

pandas/tests/frame/methods/test_reset_index.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -337,24 +337,19 @@ def test_reset_index_multiindex_nan(self):
337337
tm.assert_frame_equal(rs, df)
338338

339339
@pytest.mark.parametrize(
340-
"name",
340+
"name, warn",
341341
[
342-
None,
343-
"foo",
344-
2,
345-
3.0,
346-
pd.Timedelta(6),
347-
Timestamp("2012-12-30", tz="UTC"),
348-
"2012-12-31",
342+
(None, UserWarning),
343+
("foo", UserWarning),
344+
(2, None),
345+
(3.0, None),
346+
(pd.Timedelta(6), None),
347+
(Timestamp("2012-12-30", tz="UTC"), FutureWarning),
348+
("2012-12-31", None),
349349
],
350350
)
351-
def test_reset_index_with_datetimeindex_cols(self, name):
351+
def test_reset_index_with_datetimeindex_cols(self, name, warn):
352352
# GH#5818
353-
warn = None
354-
if isinstance(name, Timestamp) and name.tz is not None:
355-
# _deprecate_mismatched_indexing
356-
warn = FutureWarning
357-
358353
df = DataFrame(
359354
[[1, 2], [3, 4]],
360355
columns=date_range("1/1/2013", "1/2/2013"),

0 commit comments

Comments
 (0)