Skip to content

BUG: fix to_datetime to handle int16 and int8 #13464

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

Closed
wants to merge 1 commit into from
Closed

BUG: fix to_datetime to handle int16 and int8 #13464

wants to merge 1 commit into from

Conversation

ravinimmi
Copy link
Contributor

@ravinimmi ravinimmi commented Jun 16, 2016

Fixes #13451

@jreback
Copy link
Contributor

jreback commented Jun 16, 2016

tests!

@ravinimmi
Copy link
Contributor Author

ravinimmi commented Jun 16, 2016

@jreback Hi, I am a bit confused about which file should the tests be written in? There is no separate file that contains tests for pandas.tseries.tools .

@jreback
Copy link
Contributor

jreback commented Jun 16, 2016

https://github.com/pydata/pandas/blob/master/pandas/tseries/tests/test_timeseries.py#L2453 or you can make another function in the same class

@jreback jreback added Bug Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions labels Jun 16, 2016
@@ -2563,6 +2563,26 @@ def test_dataframe(self):
with self.assertRaises(ValueError):
to_datetime(df2)

def test_dataframe_dtypes(self):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number here as a comment

@jreback
Copy link
Contributor

jreback commented Jun 16, 2016

lgtm (minor comment), pls add a whatsnew entry. ping when green.

@jreback jreback added this to the 0.18.2 milestone Jun 16, 2016
@@ -510,6 +510,9 @@ def coerce(values):
# we allow coercion to if errors allows
return to_numeric(values, errors=errors)

# prevent overflow in case of int8 or int16
arg = arg.astype('int64', copy=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should cause another problem. On current master:

df = pd.DataFrame({'year': [2000, 2001], 'month': [1.5, 1], 'day': [1, 1]})
pd.to_datetime(df)
# ValueError: cannot assemble the datetimes: 'float' object is unsliceable

pd.to_datetime(df.astype(np.int64))
# 0   2000-01-01
# 1   2001-01-01
# dtype: datetime64[ns]

Better to raise if input contains non-integer dtype.

Copy link
Contributor

@jreback jreback Jun 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. @ravinimmi can you add checking that the passes dtypes are all integer.

use is_integer_dtype, and if not raise (need tests as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback What should be the output in case of string type ? Or should it raise ValueError?

df = pd.DataFrame({'year': [2000, 2001], 'month': [1, 1], 'day': [1, 1]})
pd.to_datetime(df.astype(str))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings are already tested / work.

internally pd.to_numeric is used which coerces non-numeric to numeric (or it raises if it fails). Thus you are left with float/int.

@codecov-io
Copy link

codecov-io commented Jun 16, 2016

Current coverage is 84.32%

Merging #13464 into master will increase coverage by <.01%

@@             master     #13464   diff @@
==========================================
  Files           138        138          
  Lines         51069      51072     +3   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          43066      43069     +3   
  Misses         8003       8003          
  Partials          0          0          

Powered by Codecov. Last updated by 013c2ce...dc4944d

@jreback jreback closed this in 9d33c7b Jun 17, 2016
@jreback
Copy link
Contributor

jreback commented Jun 17, 2016

thanks!

@ravinimmi ravinimmi deleted the bugfix branch June 18, 2016 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to_datetime can't handle int16 or int8
4 participants