From 67635fa580803d2a1a504f1f5b6c489d2cc59833 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 13 Nov 2019 13:45:41 +0200 Subject: [PATCH 1/4] @Willayd solution [ref](https://github.com/pandas-dev/pandas/pull/29554#discussion_r344998228) --- pandas/_libs/tslib.pyx | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 01d90900cd604..2b1fbe3129a59 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -266,20 +266,16 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None, elif basic_format: dt64_to_dtstruct(val, &dts) - res = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year, - dts.month, - dts.day, - dts.hour, - dts.min, - dts.sec) + res = (f'{dts.year}-{dts.month:02d}-{dts.day:02d} ' + f'{dts.hour:02d}:{dts.min:02d}:{dts.sec:02d}') if show_ns: ns = dts.ps // 1000 - res += '.%.9d' % (ns + 1000 * dts.us) + res += f'.{ns + dts.us * 1000:09d}' elif show_us: - res += '.%.6d' % dts.us + res += f'.{dts.us:06d}' elif show_ms: - res += '.%.3d' % (dts.us / 1000) + res += f'{dts.us / 1000:03d}' result[i] = res From 7d7f215eec5c2831c484a57c17071b21f92eb287 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 13 Nov 2019 14:32:14 +0200 Subject: [PATCH 2/4] @Willayd solution [ref](https://github.com/pandas-dev/pandas/pull/29554#discussion_r344998228) --- pandas/_libs/tslib.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 2b1fbe3129a59..e8087afcdf77b 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -275,7 +275,7 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None, elif show_us: res += f'.{dts.us:06d}' elif show_ms: - res += f'{dts.us / 1000:03d}' + res += f'.{dts.us / 1000:03d}' result[i] = res From 6c9892e28b1e2567e45af7d4a3a2e48ac453a194 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 13 Nov 2019 15:39:51 +0200 Subject: [PATCH 3/4] Passing failed tests --- pandas/_libs/tslib.pyx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index e8087afcdf77b..08cd64c601a79 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -266,16 +266,16 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None, elif basic_format: dt64_to_dtstruct(val, &dts) - res = (f'{dts.year}-{dts.month:02d}-{dts.day:02d} ' - f'{dts.hour:02d}:{dts.min:02d}:{dts.sec:02d}') + res = (f'{dts.year}-{int(dts.month):02}-{int(dts.day):02} ' + f'{int(dts.hour):02}:{int(dts.min):02}:{int(dts.sec):02}') if show_ns: ns = dts.ps // 1000 - res += f'.{ns + dts.us * 1000:09d}' + res += f'.{int(ns + dts.us * 1000):09}' elif show_us: - res += f'.{dts.us:06d}' + res += f'.{int(dts.us):06}' elif show_ms: - res += f'.{dts.us / 1000:03d}' + res += f'.{int(dts.us / 1000):03}' result[i] = res From 70787319e4875ad56cf7d1d27bbe8894263d7ded Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 13 Nov 2019 23:12:55 +0200 Subject: [PATCH 4/4] Fixes to review by @jreback --- pandas/_libs/tslib.pyx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 08cd64c601a79..598def4e1d9fa 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -266,16 +266,16 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None, elif basic_format: dt64_to_dtstruct(val, &dts) - res = (f'{dts.year}-{int(dts.month):02}-{int(dts.day):02} ' - f'{int(dts.hour):02}:{int(dts.min):02}:{int(dts.sec):02}') + res = (f'{dts.year}-{dts.month:02d}-{dts.day:02d} ' + f'{dts.hour:02d}:{dts.min:02d}:{dts.sec:02d}') if show_ns: ns = dts.ps // 1000 - res += f'.{int(ns + dts.us * 1000):09}' + res += f'.{ns + dts.us * 1000:09d}' elif show_us: - res += f'.{int(dts.us):06}' + res += f'.{dts.us:06d}' elif show_ms: - res += f'.{int(dts.us / 1000):03}' + res += f'.{dts.us // 1000:03d}' result[i] = res