Skip to content

BUG: Fix DatetimeIndex.insert() with strings. #5819

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

Merged

Conversation

jtratner
Copy link
Contributor

@jtratner jtratner commented Jan 2, 2014

Falls back to object Index instead. (previously wasn't checking for them), but only strings are allowed.

Fixes #5818.

@jtratner
Copy link
Contributor Author

jtratner commented Jan 2, 2014

This fails tests because of restrictions on partial setting from #4940. I'm not actually convinced that the tests are correct (or at the very least the issue needs to be handled elsewhere). I'll circle back to this tomorrow.

This PR makes DatetimeIndex match Int64Index:

In [2]: ind = pd.Index(range(10))

In [3]: ind.insert(3, '5')
Out[3]: Index([0, 1, 2, u'5', 3, 4, 5, 6, 7, 8, 9], dtype='object')

I'm not clear on why partial setting that has incompatible dtype labels is actually wrong, especially because you can currently do it with non-DatetimeIndex:

In [4]: df = pd.DataFrame(range(10))

In [5]: df
Out[5]:
   0
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9

[10 rows x 1 columns]

In [6]: df.loc['apple', :] = 2

In [7]: df
Out[7]:
       0
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
apple  2

[11 rows x 1 columns]

@jtratner
Copy link
Contributor Author

jtratner commented Jan 2, 2014

You could end up with something similar by concatenating DataFrames with different Index dtypes or by doing a join, so not sure it really makes sense to block partial setting via loc/ix.

@jreback
Copy link
Contributor

jreback commented Jan 2, 2014

jtratner#7

@jtratner
Copy link
Contributor Author

jtratner commented Jan 2, 2014

Just to summarize @jreback 's resolution for those who don't want to follow the link:

  1. You can't concatenate floats/ints to DatetimeIndex, because internally DI is represented by int64, so it's too likely you'd get something wrong.
  2. You are allowed to concatenate strings.
  3. Concatenating something like the string '2001-01-01' should probably also result in object Index

@jreback
Copy link
Contributor

jreback commented Jan 2, 2014

@jtratner just need a release note....then go ahead and merge..thxs

@jreback
Copy link
Contributor

jreback commented Jan 15, 2014

@jtratner this looked fine...thought it was merged...

can you rebase and merge it?

@jreback
Copy link
Contributor

jreback commented Jan 17, 2014

@jtratner ?

@ghost ghost assigned jtratner Jan 17, 2014
@jreback
Copy link
Contributor

jreback commented Jan 21, 2014

@jtratner ?

@jreback jreback merged commit 3204652 into pandas-dev:master Jan 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DatetimeIndex columns cause reset_index() to throw AttributeError
2 participants