Skip to content

PERF: write basic datetimes faster #10271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Improved ``Series.resample`` performance with dtype=datetime64[ns] (:issue:`7754`)
- Modest improvement in datetime writing speed in to_csv (:issue:`10271`)

.. _whatsnew_0162.bug_fixes:

Expand Down
47 changes: 36 additions & 11 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray,
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA)
import numpy as np

from cpython.ref cimport PyObject
from cpython cimport (
PyTypeObject,
PyFloat_Check,
PyLong_Check,
PyObject_RichCompareBool,
PyObject_RichCompare,
PyString_Check,
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE,
)

# Cython < 0.17 doesn't have this in cpython
cdef extern from "Python.h":
cdef PyTypeObject *Py_TYPE(object)
int PySlice_Check(object)
object PyUnicode_FromFormat(const char*, ...)

cdef extern from "datetime_helper.h":
double total_seconds(object)
Expand Down Expand Up @@ -1450,20 +1452,43 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None, object f
elif basic_format:

pandas_datetime_to_datetimestruct(val, PANDAS_FR_ns, &dts)
res = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec)

if show_ns:
ns = dts.ps / 1000
res += '.%.9d' % (ns + 1000 * dts.us)
res = PyUnicode_FromFormat('%d-%02d-%02d %02d:%02d:%02d.%09d',
dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec,
ns + 1000 * dts.us)
elif show_us:
res += '.%.6d' % dts.us
res = PyUnicode_FromFormat('%d-%02d-%02d %02d:%02d:%02d.%06d',
dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec,
dts.us)

elif show_ms:
res += '.%.3d' % (dts.us/1000)
res = PyUnicode_FromFormat('%d-%02d-%02d %02d:%02d:%02d.%03d',
dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec,
dts.us/1000)
else:
res = PyUnicode_FromFormat('%d-%02d-%02d %02d:%02d:%02d',
dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec)

result[i] = res

Expand Down