Skip to content

Type error on using datetime64's microseconds in datetime.timedelta #10050

Closed
@miraculixx

Description

@miraculixx

Using Pandas' datetime64 dtype to convert to a datetime.timedelta object results in this type error:

TypeError: unsupported type for timedelta microseconds component: numpy.int32

Reproduce:

# set up a dataframe with a datetime value in it
from datetime import datetime
import pandas as pd
df = pd.DataFrame({'foo' : [datetime.now()] })
print df.dtypes
# =>
foo    datetime64[ns]
dtype: object
# let's convert to timedelta
df.foo.apply(lambda r : timedelta(hours = r.hour, minutes = r.minute, seconds = r.second, microseconds = r.microseconds))
=> TypeError: unsupported type for timedelta microseconds component: numpy.int32

Expected:

# no excepetions, whatever the correct value
df.foo.apply(lambda r : timedelta(hours = r.hour, minutes = r.minute, seconds = r.second, microseconds = r.microseconds))
# => 
0   16:08:25.347571
Name: foo, dtype: timedelta64[ns]

Work around:

# if you can't control the timedelta conversion (i.e. it is somewhere embedded, e.g. in a library)
df.foo = df.foo.astype(datetime)
# if you can
df.foo.apply(lambda r : timedelta(hours = r.hour, minutes = r.minute, seconds = r.second, microseconds = int(r.microseconds))

Version information

> pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-24-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.16.0
nose: None
Cython: None
numpy: 1.9.2
scipy: None
statsmodels: None
IPython: 3.0.0
sphinx: 1.2.2
patsy: None
dateutil: 2.3
pytz: 2014.10
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: 2.1.4
xlrd: None
xlwt: None
xlsxwriter: None
lxml: 3.4.2
bs4: 4.3.2
html5lib: 0.999
httplib2: 0.8
apiclient: 1.2
sqlalchemy: None
pymysql: None
psycopg2: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeDtype ConversionsUnexpected or buggy dtype conversions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions