Skip to content
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

dtype changed when DataFrame add DateOffset #21610

Closed
holymonson opened this issue Jun 23, 2018 · 3 comments

Comments

Projects
None yet
5 participants
@holymonson
Copy link
Contributor

commented Jun 23, 2018

Code Sample, a copy-pastable example if possible

pd.DataFrame([pd.to_datetime('2018')]).dtypes
# 0    datetime64[ns]
# dtype: object
(pd.DataFrame([pd.to_datetime('2018')]) + pd.DateOffset(years=1)).dtypes
# 0    object
# dtype: object

Problem description

Timestamp + DateOffset = Timestamp, but DataFrame(datetime64[ns]) + DateOffset = DataFrame(object).

Expected Output

DataFrame(datetime64[ns]) + DateOffset = DataFrame(datetime64[ns]). Don't change dtype.

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Darwin
OS-release: 17.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: zh_CN.UTF-8
LOCALE: zh_CN.UTF-8

pandas: 0.23.0
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: None
sphinx: 1.6.2
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.8.0
bs4: 4.5.3
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@chris-b1

This comment has been minimized.

Copy link
Contributor

commented Jun 23, 2018

Yeah, I guess the dispatch on this only works for Series, this should also work.

In [19]: df[0] = df[0] + pd.DateOffset(years=1)

In [20]: df
Out[20]:
           0
0 2019-01-01

In [21]: df.dtypes
Out[21]:
0    datetime64[ns]
dtype: object

cc @jbrockmendel (anything open that already covers this?)

@jbrockmendel

This comment has been minimized.

Copy link
Member

commented Jun 23, 2018

anything open that already covers this?

I don't think so. Just added a note to #18824.

@holymonson

This comment has been minimized.

Copy link
Contributor Author

commented Jul 21, 2018

# coerce/transpose the args if needed
try:
values, values_mask, other, other_mask = self._try_coerce_args(
transf(values), other)
except TypeError:
block = self.coerce_to_target_dtype(orig_other)
return block.eval(func, orig_other,
errors=errors,
try_cast=try_cast, mgr=mgr)

DateOffset is dtype object, a DatetimeBlock operated with object will cast to ObjectBlock.

Besides, pd.DataFrame([pd.Timestamp('2018')]) + pd.Timedelta('1d') fails, but it seems easier to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.