0

I've the following code:

def excel_date(date1):
    temp = datetime.datetime(1899, 12, 30)
    delta = date1 - temp  if date1 != 0 else temp - temp
    return float(delta.days) + (float(delta.seconds) / 86400)

df3['SuperID'] = df3['Break_date'].apply(excel_date)
df3['SuperID2'] = df3['ticker'] + str(df3['SuperID'])

Where I use a date to insert in date1 and I get a number from the excel date function.

My ticker and SuperID fields are OK:

df3 print

I want to concatenate both and get TSLA44462 BUT it's concatenating the whole series if I use str() or .astype(str) in my SuperID column.

The column types:

print dtypes

Timus
  • 10,974
  • 5
  • 14
  • 28
Artrade
  • 49
  • 5
  • 1
    Please add details of the `df3` dictionary in your question. As it currently is, it is very difficult to understand what this `df3` looks like, and how is it being populated; and is it even necessary. – akaAbdullahMateen Jul 17 '22 at 19:45
  • Does this help you https://stackoverflow.com/questions/11858472/string-concatenation-of-two-pandas-columns – Carlos Horn Jul 18 '22 at 07:43

2 Answers2

1

Here my solution if I understood your problem :

import pandas as pd
df = pd.DataFrame({"Col1":[1.0,2.0,3.0,4.4], "Col2":["Michel", "Sardou", "Paul", "Jean"], "Other Col":[2,3,5,2]})
df["Concat column"] = df["Col1"].astype(int).astype(str) + df["Col2"]
df[df["Concat column"] == "1Michel"]

or

df = pd.DataFrame({"Col1":[1.0,2.0,3.0,4.4], "Col2":["Michel", "Sardou", "Paul", "Jean"], "Other Col":[2,3,5,2]})
df[(df["Col1"]==1) & (df["Col2"]=="Michel")]
Virgaux Pierre
  • 207
  • 1
  • 4
0

After some hours of investigation and the help of comments the way to work with series, integers, floats and strings which worked for me is this:

    def excel_date(date1):
        temp = datetime.datetime(1899, 12, 30)
        delta = date1 - temp  if date1 != 0 else temp - temp
        return float(delta.days) + (float(delta.seconds) / 86400)

First of all I convert float to integer to avoid decimals. int(x) is not feasible for series, so you better use .astype(int) which works fine.

    df3['SuperID'] = df3['Break_date'].apply(excel_date).astype(int)

After that, convert everything to char with char.array and not str(x) or .astype. You then just need to sum columns using .astype(str) to get the desired result.

    a = np.char.array(df3['ticker'].values)
    b = np.char.array(df3['SuperID'].values)
    df3['SuperID2'] = (a + b).astype(str)

Hope this help to others working with series.

regards

Artrade
  • 49
  • 5