Exemple #1
0
 def _convert_tolerance(self, tolerance):
     try:
         return tslib.Timedelta(tolerance).to_timedelta64()
     except ValueError:
         raise ValueError('tolerance argument for %s must be convertible '
                          'to Timedelta: %r' %
                          (type(self).__name__, tolerance))
def _coerce_scalar_to_timedelta_type(r, unit='ns', box=True, errors='raise'):
    """ convert strings to timedelta; coerce to Timedelta (if box), else np.timedelta64"""

    result = tslib.convert_to_timedelta(r,unit,errors)
    if box:
        result = tslib.Timedelta(result)

    return result
Exemple #3
0
def _maybe_box_datetimelike(value):
    # turn a datetime like into a Timestamp/timedelta as needed

    if isinstance(value, (np.datetime64, datetime)):
        value = tslib.Timestamp(value)
    elif isinstance(value, (np.timedelta64, timedelta)):
        value = tslib.Timedelta(value)

    return value
Exemple #4
0
def _coerce_scalar_to_timedelta_type(r, unit='ns', box=True, coerce=False):
    """ convert strings to timedelta; coerce to Timedelta (if box), else np.timedelta64"""

    if isinstance(r, compat.string_types):

        # we are already converting to nanoseconds
        converter = _get_string_converter(r, unit=unit)
        r = converter()
        unit = 'ns'

    result = tslib.convert_to_timedelta(r, unit, coerce)
    if box:
        result = tslib.Timedelta(result)

    return result
Exemple #5
0
def _coerce_scalar_to_timedelta_type(r, unit='ns', box=True, errors='raise'):
    """Convert string 'r' to a timedelta object."""

    try:
        result = tslib.convert_to_timedelta64(r, unit)
    except ValueError:
        if errors == 'raise':
            raise
        elif errors == 'ignore':
            return r

        # coerce
        result = pd.NaT

    if box:
        result = tslib.Timedelta(result)
    return result