From dc94c1398850420a20201bfdcc53042f33e294e8 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 23 Dec 2019 21:18:54 -0500 Subject: [PATCH 1/5] Replaced .format{}/% with f-strings Modifeid to python3 format strings ref: https://github.com/pandas-dev/pandas/issues/29547 --- pandas/core/tools/datetimes.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 2d43623cec92c..ed8048e9138ca 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -475,8 +475,8 @@ def _adjust_to_origin(arg, origin, unit): j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): raise tslibs.OutOfBoundsDatetime( - "{original} is Out of Bounds for " - "origin='julian'".format(original=original) + f"{original} is Out of Bounds for " + "origin='julian'" ) else: # arg must be numeric @@ -485,10 +485,8 @@ def _adjust_to_origin(arg, origin, unit): or is_numeric_dtype(np.asarray(arg)) ): raise ValueError( - "'{arg}' is not compatible with origin='{origin}'; " - "it must be numeric with a unit specified ".format( - arg=arg, origin=origin - ) + f"'{arg}' is not compatible with origin='{origin}'; " + "it must be numeric with a unit specified " ) # we are going to offset back to unix / epoch time @@ -496,16 +494,16 @@ def _adjust_to_origin(arg, origin, unit): offset = Timestamp(origin) except tslibs.OutOfBoundsDatetime: raise tslibs.OutOfBoundsDatetime( - "origin {origin} is Out of Bounds".format(origin=origin) + f"origin {origin} is Out of Bounds" ) except ValueError: raise ValueError( - "origin {origin} cannot be converted " - "to a Timestamp".format(origin=origin) + f"origin {origin} cannot be converted " + "to a Timestamp" ) if offset.tz is not None: - raise ValueError("origin offset {} must be tz-naive".format(offset)) + raise ValueError(f"origin offset {offset} must be tz-naive") offset -= Timestamp(0) # convert the offset to the unit of the arg @@ -810,8 +808,8 @@ def f(value): if len(req): raise ValueError( "to assemble mappings requires at least that " - "[year, month, day] be specified: [{required}] " - "is missing".format(required=",".join(req)) + f"[year, month, day] be specified: [{','.join(req)}] " + "is missing" ) # keys we don't recognize @@ -820,7 +818,7 @@ def f(value): raise ValueError( "extra keys have been passed " "to the datetime assemblage: " - "[{excess}]".format(excess=",".join(excess)) + f"[{','.join(excess)}]" ) def coerce(values): @@ -983,9 +981,9 @@ def _convert_listlike(arg, format): except (ValueError, TypeError): if errors == "raise": msg = ( - "Cannot convert {element} to a time with given " - "format {format}" - ).format(element=element, format=format) + f"Cannot convert {element} to a time with given " + f"format {format}" + ) raise ValueError(msg) elif errors == "ignore": return arg @@ -1012,7 +1010,7 @@ def _convert_listlike(arg, format): times.append(time_object) elif errors == "raise": raise ValueError( - "Cannot convert arg {arg} to a time".format(arg=arg) + f"Cannot convert arg {arg} to a time" ) elif errors == "ignore": return arg From 63fd66d2fead9179b2d346e355896a11d74a16ac Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 23 Dec 2019 21:54:58 -0500 Subject: [PATCH 2/5] Update from black auot-formatter `black pandas` altered the code formatting --- pandas/core/tools/datetimes.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index ed8048e9138ca..a2a1dd4ff5238 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -475,8 +475,7 @@ def _adjust_to_origin(arg, origin, unit): j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): raise tslibs.OutOfBoundsDatetime( - f"{original} is Out of Bounds for " - "origin='julian'" + f"{original} is Out of Bounds for " "origin='julian'" ) else: # arg must be numeric @@ -493,14 +492,9 @@ def _adjust_to_origin(arg, origin, unit): try: offset = Timestamp(origin) except tslibs.OutOfBoundsDatetime: - raise tslibs.OutOfBoundsDatetime( - f"origin {origin} is Out of Bounds" - ) + raise tslibs.OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") except ValueError: - raise ValueError( - f"origin {origin} cannot be converted " - "to a Timestamp" - ) + raise ValueError(f"origin {origin} cannot be converted " "to a Timestamp") if offset.tz is not None: raise ValueError(f"origin offset {offset} must be tz-naive") @@ -1009,9 +1003,7 @@ def _convert_listlike(arg, format): if time_object is not None: times.append(time_object) elif errors == "raise": - raise ValueError( - f"Cannot convert arg {arg} to a time" - ) + raise ValueError(f"Cannot convert arg {arg} to a time") elif errors == "ignore": return arg else: From 44aea34bd0a1bfd62426cb6a64224f2e05d2450b Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 23 Dec 2019 21:55:46 -0500 Subject: [PATCH 3/5] Revert "Update from black auto-formatter" This reverts commit 63fd66d2fead9179b2d346e355896a11d74a16ac. --- pandas/core/tools/datetimes.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index a2a1dd4ff5238..ed8048e9138ca 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -475,7 +475,8 @@ def _adjust_to_origin(arg, origin, unit): j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): raise tslibs.OutOfBoundsDatetime( - f"{original} is Out of Bounds for " "origin='julian'" + f"{original} is Out of Bounds for " + "origin='julian'" ) else: # arg must be numeric @@ -492,9 +493,14 @@ def _adjust_to_origin(arg, origin, unit): try: offset = Timestamp(origin) except tslibs.OutOfBoundsDatetime: - raise tslibs.OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") + raise tslibs.OutOfBoundsDatetime( + f"origin {origin} is Out of Bounds" + ) except ValueError: - raise ValueError(f"origin {origin} cannot be converted " "to a Timestamp") + raise ValueError( + f"origin {origin} cannot be converted " + "to a Timestamp" + ) if offset.tz is not None: raise ValueError(f"origin offset {offset} must be tz-naive") @@ -1003,7 +1009,9 @@ def _convert_listlike(arg, format): if time_object is not None: times.append(time_object) elif errors == "raise": - raise ValueError(f"Cannot convert arg {arg} to a time") + raise ValueError( + f"Cannot convert arg {arg} to a time" + ) elif errors == "ignore": return arg else: From 88dfd335eb161e5a1756dc4141c3769c0cc29412 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 23 Dec 2019 22:03:50 -0500 Subject: [PATCH 4/5] Update from black auto-formatter to datetimes.py `black pandas` auto-formatting update. Changes line breaks due to f-strings being more concise. Some of the changes were stupid and I have taken out the extra `" "` on single line `raise` statements. --- pandas/core/tools/datetimes.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index ed8048e9138ca..d17dc165395fc 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -475,8 +475,7 @@ def _adjust_to_origin(arg, origin, unit): j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): raise tslibs.OutOfBoundsDatetime( - f"{original} is Out of Bounds for " - "origin='julian'" + f"{original} is Out of Bounds for origin='julian'" ) else: # arg must be numeric @@ -493,14 +492,9 @@ def _adjust_to_origin(arg, origin, unit): try: offset = Timestamp(origin) except tslibs.OutOfBoundsDatetime: - raise tslibs.OutOfBoundsDatetime( - f"origin {origin} is Out of Bounds" - ) + raise tslibs.OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") except ValueError: - raise ValueError( - f"origin {origin} cannot be converted " - "to a Timestamp" - ) + raise ValueError(f"origin {origin} cannot be converted to a Timestamp") if offset.tz is not None: raise ValueError(f"origin offset {offset} must be tz-naive") @@ -1009,9 +1003,7 @@ def _convert_listlike(arg, format): if time_object is not None: times.append(time_object) elif errors == "raise": - raise ValueError( - f"Cannot convert arg {arg} to a time" - ) + raise ValueError(f"Cannot convert arg {arg} to a time") elif errors == "ignore": return arg else: From ba0ffa0fd94f19dbbb960406427e3f00efa410f4 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 23 Dec 2019 23:48:49 -0500 Subject: [PATCH 5/5] Updated in response to comments remove trailing whitespace, join() not performed inline in f-strings. update passes `black pandas` test --- pandas/core/tools/datetimes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index d17dc165395fc..f193865d90b71 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -485,7 +485,7 @@ def _adjust_to_origin(arg, origin, unit): ): raise ValueError( f"'{arg}' is not compatible with origin='{origin}'; " - "it must be numeric with a unit specified " + "it must be numeric with a unit specified" ) # we are going to offset back to unix / epoch time @@ -800,19 +800,19 @@ def f(value): required = ["year", "month", "day"] req = sorted(set(required) - set(unit_rev.keys())) if len(req): + required = ",".join(req) raise ValueError( "to assemble mappings requires at least that " - f"[year, month, day] be specified: [{','.join(req)}] " + f"[year, month, day] be specified: [{required}] " "is missing" ) # keys we don't recognize excess = sorted(set(unit_rev.keys()) - set(_unit_map.values())) if len(excess): + excess = ",".join(excess) raise ValueError( - "extra keys have been passed " - "to the datetime assemblage: " - f"[{','.join(excess)}]" + f"extra keys have been passed to the datetime assemblage: [{excess}]" ) def coerce(values):