Open Alpima-Quant opened 3 years ago
Thanks for the report! Doing this on master, one also gets the message:
FutureWarning: Indexing a DataFrame with a datetimelike index using a single string to slice the rows, like
frame[string]
, is deprecated and will be removed in a future version. Useframe.loc[string]
instead.
It appears that once this is removed, this issue will be resolved as well.
Part of this is also that the Timestamp parser (via the dateutil parser) is interpreting 'T 1.75 1/3' as a datetime when it probably shouldn't
>>> pd.Timestamp('T 1.75 1/3')
Timestamp('2003-01-01 00:00:00')
[x] I have checked that this issue has not already been reported.
[x] I have confirmed this bug exists on the latest version of pandas.
[ ] (optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Problem description
When using
__setitem__
or__getitem__
with a str key on a pandas DataFrame where the index has ._supports_partial_string_indexing = True, pandas first tries to convert the str key to a slice using index._get_string_slice(key).It seems to me that we have too loose of an interpretation of what one of these partial string slices looks like (or perhaps when / how one might use them).
In my use case I have some timeseries data for some US treasury bills, where the names are something like f"T {coupon} {date}", e.g. "T 1.75 1/3" and I want to assign a new column to a DataFrame with some data on this instrument. However
df['T 1.75 1/3'] = value
- as in the example above - raises "ValueError: cannot set using a slice indexer with a different length than the value"Expected Output
A new column with label "T 1.75 1/3" and values [1, 2] is assigned to the pandas DataFrame
df
.Output of
pd.show_versions()