Typeerror: Cannot Concatenate Object of Type '<Class 'Str'>'; Only Series and Dataframe Objs Are Valid

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

You can use itertools.product to build a set of all of the pairs, filter to remove when the source and destination are the same location, and then construct a new data frame.

import pandas as pd
from itertools import product

df = pd.DataFrame({'sources': [
"CAULAINCOURT",
"MARCHE DE L'EUROPE",
"AU MAIRE"
]})

df_pairs = pd.DataFrame(
filter(lambda x: x[0]!=x[1], product(df.sources, df.sources)),
columns=['source', 'dest']
)

df_pairs
# returns:
source dest
0 CAULAINCOURT MARCHE DE L'EUROPE
1 CAULAINCOURT AU MAIRE
2 MARCHE DE L'EUROPE CAULAINCOURT
3 MARCHE DE L'EUROPE AU MAIRE
4 AU MAIRE CAULAINCOURT
5 AU MAIRE MARCHE DE L'EUROPE


TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid I got this error

Your code can be updated in the following way:

import pandas as pd

# set data
data = {"colummen1": ["Kenbele1", "Kenbele1", "Kenbele1", "Kenbele1", "Kenbele2", "Kenbele2", "Kenbele2", "Kenbele2"],
"colummun2": ["Commutity1", "Commutity2", "Commutity3", "Commutity4", "Commutity1", "Commutity2", "Commutity3", "Commutity4"]}

# create dataframe
df = pd.DataFrame(data)

dfs = df.groupby('colummen1')

dfs_updated = []

for _, df in dfs:
df['id'] = df.groupby(['colummen1','colummun2']).ngroup()+1
dfs_updated.append(df)

df_new = pd.concat(dfs_updated)

df_new

Returns

enter image description here

TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid

s1 is already a list. Doing what you did called pd.concat with a list of a list with DataFrames, which pandas doesn't allow. You should do it like this instead:

s2=pd.concat(s1)

TypeError: cannot concatenate object of type '<class 'yfinance.ticker.Options'>'; only Series and DataFrame objs are valid

Sequential appends to a DataFrame are extremely costly as it requires a new DataFrame to be build every iteration. For this reason, they are generally avoided. Since option_chain returns an iterable, instead of appending to the list we should extend the list. Then perform a single concat at the end.

import pandas as pd
import yfinance as yf

tickers = ['AAPL', 'AA', 'AAL']

opts_list = []

for symbol in tickers:
try:
ticker = yf.Ticker(symbol)
opt = ticker.option_chain('2021-07-30')
opts_list.extend(opt)
except ValueError:
continue

new_df = pd.concat(opts_list)

new_df:

         contractSymbol       lastTradeDate  ...  contractSize  currency
0 AAPL210730C00065000 2021-07-28 19:37:45 ... REGULAR USD
1 AAPL210730C00070000 2021-07-22 18:17:27 ... REGULAR USD
2 AAPL210730C00075000 2021-07-28 17:19:38 ... REGULAR USD
3 AAPL210730C00080000 2021-07-22 14:59:05 ... REGULAR USD
4 AAPL210730C00085000 2021-07-27 16:09:57 ... REGULAR USD
.. ... ... ... ... ...
28 AAL210730P00029000 2021-07-26 13:31:18 ... REGULAR USD
29 AAL210730P00029500 2021-07-26 13:32:22 ... REGULAR USD
30 AAL210730P00030000 2021-07-22 16:52:08 ... REGULAR USD
31 AAL210730P00031000 2021-07-22 15:53:55 ... REGULAR USD
32 AAL210730P00032000 2021-07-26 13:30:11 ... REGULAR USD

[253 rows x 14 columns]

Python Loop[ Today Stock Data

I think you need:

L = ['AAPL', 'MSFT']

increased_symbol = []

for stock in L:
daily_data = pd.DataFrame()
daily_data = daily_data.ta.ticker(stock, start="2020-01-01", end="2021-09-30")
daily_data.ta.stochrsi(append=True)
daily_data = daily_data.iloc[-1:]
#append dataframes to list
increased_symbol.append(daily_data)

#concat list of DataFrames
df = pd.concat(increased_symbol)
print(df)
Open High Low Close Volume \
Date
2021-09-29 142.470001 144.449997 142.029999 142.830002 74602000
2021-09-29 285.100006 286.769989 283.010010 284.000000 26353700

Dividends Stock Splits STOCHRSIk_14_14_3_3 STOCHRSId_14_14_3_3
Date
2021-09-29 0.0 0.0 18.203772 22.490302
2021-09-29 0.0 0.0 0.839166 13.396303

If need also ticker in column:

L = ['AAPL', 'MSFT']

increased_symbol = []

for stock in L:
daily_data = pd.DataFrame()
daily_data = daily_data.ta.ticker(stock, start="2020-01-01", end="2021-09-30")
daily_data.ta.stochrsi(append=True)
daily_data = daily_data.iloc[-1:]
increased_symbol.append(daily_data.assign(ticker=stock))


df = pd.concat(increased_symbol)
print(df)

Open High Low Close Volume \
Date
2021-09-29 142.470001 144.449997 142.029999 142.830002 74602000
2021-09-29 285.100006 286.769989 283.010010 284.000000 26353700

Dividends Stock Splits STOCHRSIk_14_14_3_3 STOCHRSId_14_14_3_3 \
Date
2021-09-29 0.0 0.0 18.203772 22.490302
2021-09-29 0.0 0.0 0.839166 13.396303

ticker
Date
2021-09-29 AAPL
2021-09-29 MSFT


Related Topics



Leave a reply



Submit