I am trying to determine, if an identifier occurs first in a given month (i.e. it is 'new' to the list of identifier). Below, there is the first attempt, but it flags identifier a3 as old on 28 Feb 2020, although it was not in the list on 31 Jan 2020.
Note that this is a simplified example: in practice, I would have more group by columns, not just date, and I would need to check, if the identifier is new to the 'cell' created by the combination of date, industry, age, etc. There could be many.
import pandas as pd, numpy as np
data = """
date identifier value
31-Dec-2019 a1 10
31-Dec-2019 a2 20
31-Dec-2019 a3 30
31-Jan-2020 a1 40
31-Jan-2020 a2 50
31-Jan-2020 a4 60
31-Jan-2020 a5 60
28-Feb-2020 a1 70
28-Feb-2020 a4 80
28-Feb-2020 a3 90
"""
res=[]
for row in [el.split() for el in data.splitlines()][1:]:
rrow=[]
for col in row:
try:
if float(col):
col = np.float32(col)
except:
pass
rrow.append(col)
res.append(rrow)
df = pd.DataFrame(data=res[1:], columns=res[0])
df.date = pd.to_datetime(df.date)
df = df.set_index(["date", "identifier"]).sort_index()
df["valprev"] = df.groupby(level="identifier")["value"].shift(1)
df["isnew"] = df.valprev.isnull(
