Skip to content

Commit 5141ef5

Browse files
committed
Merge pull request #8557 from jreback/dt_perf
PERF: fixup datetime_index repr perf (GH8556)
2 parents e9f55a3 + 857042f commit 5141ef5

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

pandas/core/format.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -2027,14 +2027,19 @@ def _format_datetime64_dateonly(x, nat_rep='NaT', date_format=None):
20272027

20282028

20292029
def _is_dates_only(values):
2030-
for d in values:
2031-
if isinstance(d, np.datetime64):
2032-
d = Timestamp(d)
2033-
2034-
if d is not None and not lib.checknull(d) and d._has_time_component():
2035-
return False
2036-
return True
2030+
# return a boolean if we are only dates (and don't have a timezone)
2031+
from pandas import DatetimeIndex
2032+
values = DatetimeIndex(values)
2033+
if values.tz is not None:
2034+
return False
20372035

2036+
values_int = values.asi8
2037+
consider_values = values_int != iNaT
2038+
one_day_nanos = (86400 * 1e9)
2039+
even_days = np.logical_and(consider_values, values_int % one_day_nanos != 0).sum() == 0
2040+
if even_days:
2041+
return True
2042+
return False
20382043

20392044
def _get_format_datetime64(is_dates_only, nat_rep='NaT', date_format=None):
20402045

vb_suite/index_object.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108

109109

110110
# Constructing MultiIndex from cartesian product of iterables
111-
#
111+
#
112112

113113
setup = common_setup + """
114114
iterables = [tm.makeStringIndex(10000), xrange(20)]
@@ -130,3 +130,14 @@
130130
Benchmark("MultiIndex.from_product([level1, level2]).values", setup,
131131
name='multiindex_with_datetime_level',
132132
start_date=datetime(2014, 10, 11))
133+
134+
#----------------------------------------------------------------------
135+
# repr
136+
137+
setup = common_setup + """
138+
dr = pd.date_range('20000101', freq='D', periods=100000)
139+
"""
140+
141+
datetime_index_repr = \
142+
Benchmark("dr._is_dates_only", setup,
143+
start_date=datetime(2012, 1, 11))

0 commit comments

Comments
 (0)