Assuming the following 3 Pandas Dataframes:
df1:
ask.c ask.h ask.l ask.o bid.c bid.h bid.l bid.o complete mid.c mid.h mid.l mid.o volume date
0 1.39146 1.39148 1.39146 1.39146 1.39138 1.39140 1.39136 1.39136 True 1.39142 1.39144 1.39141 1.39141 5 2014-03-19 09:00:00
. .
. .
. .
df2:
ask.c ask.h ask.l ask.o bid.c bid.h bid.l bid.o complete mid.c mid.h mid.l mid.o volume date
0 1.39147 1.39148 1.39147 1.39147 1.39138 1.39138 1.39137 1.39137 True 1.39142 1.39142 1.39142 1.39142 4 2014-03-19 09:00:05
1 1.39149 1.39149 1.39148 1.39149 1.39138 1.39141 1.39138 1.39141 True 1.39143 1.39145 1.39143 1.39145 3 2014-03-19 09:00:10
. .
. .
. .
df is built with:
df = pandas.concat([df1,df2], ignore_index=True)
df.drop_duplicates("date", keep="first", inplace=True)
df["date"] = pd.to_datetime(df["date"])
df = df.set_index("date")
Why is df["ask.c"]["2014-03-19 09:00:00"]
returning a Panda Series instead of a scalar/string value ?
print(type(df1["ask.c"]["2014-03-19 09:00:00"]))¬
<class 'pandas.core.series.Series'>
print(df1["ask.c"]["2014-03-19 09:00:00"])¬
Date
2014-03-19 09:00:00 1.39146
Name: ask.c, dtype: object
EDIT:
@IIya V.Schurov's explanation got me on the right track but we are not quite fully there yet. Pandas has something called "DatetimeIndex Partial String Indexing" which is a form slicing.
Hence the expression df["2016-12-07"]
would return a series rather than a scalar. The documentation mentions that df["2016-12-07 09:00:00"] is
not a slice nor resolve to one and will raise a KeyError
but in my case, it does not raise any KeyError and does resolve like a slice.