Reputation: 469
TL;DR: .loc[] returns DataFrame type all the time. Even when specifying single index.
I've tried everything. This is driving me insane.
I can't seem to reproduce it anywhere else.
I've checked every Type of data that's beeing passed. Everything is as it should be. But no matter what I pass into the .loc[] it will return a DataFrame not series.
import numpy as np
import pandas as pd
import datetime
index_list = 'A B C D E F G H'.split()
df = pd.DataFrame(data=None,index=index_list)
k = 0
while k <= 2:
now = datetime.datetime.now().strftime('%H:%M:%S')
df.loc[:,now] = 1
for i in index_list:
print(df.loc[i])
print(type(df.loc[i]))
k += 1
The code above will run with 0 errors and return Series Type of data. This is distilled code but it's exactly the same as the real one. Same flow, exaclty the same Type of data is beeing passed.
The problem is that in the real script .loc will only return DataFrame type not Series. And I have no idea why. I even tried to manually enter the .loc index name to check if I'm not passing the wrong type of data. Still returned DataFrame.
I'm 100% out of ideas of what I could be doing wrong.
Maybe some of you have ideas?
EDIT
removed The original code.
I found that if I call print(df.loc[i].iloc[0]) It will return the Series data for the column.
print(type(df.loc[i].iloc[0]))
Will print:
20:48:48 1
Name: (A,), dtype: int64
<class 'pandas.core.series.Series'>
Why is the name (A,) a tuple?
Upvotes: 1
Views: 1312
Reputation: 353149
TLDR: remove your extra brackets when building dfCoinMaster's index.
In the working code:
df = pd.DataFrame(data=None,index=index_list)
In the non-working code:
dfCoinMaster = pd.DataFrame(data=None,index=[current_coin_listings])
You're adding an extra level of list nesting, which you can see in your
Name: (A,), dtype: int64
line. You can reproduce the same behaviour by adding the extra brackets to your test:
In [28]: df = pd.DataFrame(data=None, index=[index_list])
In [29]: df.loc[:, 'test'] = 10
In [30]: df
Out[30]:
test
A 10
B 10
C 10
D 10
E 10
F 10
G 10
H 10
In [31]: df.loc['A']
Out[31]:
test
A 10
In [32]: type(_)
Out[32]: pandas.core.frame.DataFrame
But:
In [33]: df.loc[('A',)]
Out[33]:
test 10
Name: (A,), dtype: int64
In [34]: type(_)
Out[34]: pandas.core.series.Series
Upvotes: 1