Skip to content

Commit 95e5989

Browse files
Ajay SaxenaAjay Saxena
Ajay Saxena
authored and
Ajay Saxena
committed
added private method for coercing data
1 parent b496947 commit 95e5989

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

pandas/tools/tile.py

+17-19
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
import pandas.core.algorithms as algos
1212
import pandas.core.nanops as nanops
1313
from pandas.compat import zip
14-
from pandas import to_timedelta
15-
from pandas import to_datetime
14+
from pandas import to_timedelta, to_datetime
1615
import numpy as np
17-
from pandas.types.common import (is_datetime64_dtype, is_timedelta64_dtype)
16+
from pandas.types.common import is_datetime64_dtype, is_timedelta64_dtype
1817

1918

2019
def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
@@ -85,14 +84,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
8584
# NOTE: this binning code is changed a bit from histogram for var(x) == 0
8685
# for handling the cut for datetime and timedelta objects
8786

88-
dtype = None
89-
if is_timedelta64_dtype(x):
90-
x = x.view(np.int64)
91-
dtype = np.timedelta64
92-
93-
elif is_datetime64_dtype(x):
94-
x = x.view(np.int64)
95-
dtype = np.datetime64
87+
original, x, dtype = _coerce_to_type(x)
9688

9789
if not np.iterable(bins):
9890
if is_scalar(bins) and bins < 1:
@@ -179,14 +171,7 @@ def qcut(x, q, labels=None, retbins=False, precision=3):
179171
>>> pd.qcut(range(5), 4, labels=False)
180172
array([0, 0, 1, 2, 3], dtype=int64)
181173
"""
182-
dtype = None
183-
if is_timedelta64_dtype(x):
184-
x = x.view(np.int64)
185-
dtype = np.timedelta64
186-
187-
elif is_datetime64_dtype(x):
188-
x = x.view(np.int64)
189-
dtype = np.datetime64
174+
original, x, dtype = _coerce_to_type(x)
190175

191176
if is_integer(q):
192177
quantiles = np.linspace(0, 1, q + 1)
@@ -329,3 +314,16 @@ def _trim_zeros(x):
329314
if len(x) > 1 and x[-1] == '.':
330315
x = x[:-1]
331316
return x
317+
318+
319+
def _coerce_to_type(x):
320+
dtype = None
321+
original = x
322+
if is_timedelta64_dtype(x):
323+
x = x.view(np.int64)
324+
dtype = np.timedelta64
325+
326+
elif is_datetime64_dtype(x):
327+
x = x.view(np.int64)
328+
dtype = np.datetime64
329+
return original, x, dtype

0 commit comments

Comments
 (0)