Pandas: slow date conversion

Note: As @ritchie46’s answer states, this solution may be redundant since pandas version 0.25 per the new argument cache_dates that defaults to True Try using this function for parsing dates: def lookup(date_pd_series, format=None): “”” This is an extremely fast approach to datetime parsing. For large data, the same dates are often repeated. Rather than re-parse … Read more

Pandas error in Python: columns must be same length as key

You need a bit modify solution, because sometimes it return 2 and sometimes only one column: df2 = pd.DataFrame({‘STATUS’:[‘Estimated 3:17 PM’,’Delayed 3:00 PM’]}) df3 = df2[‘STATUS’].str.split(n=1, expand=True) df3.columns = [‘STATUS_ID{}’.format(x+1) for x in df3.columns] print (df3) STATUS_ID1 STATUS_ID2 0 Estimated 3:17 PM 1 Delayed 3:00 PM df2 = df2.join(df3) print (df2) STATUS STATUS_ID1 STATUS_ID2 0 … Read more

How to convert a list of Pydantic BaseModels to Pandas Dataframe

A quick and dirty profiling yield the following values: from pydantic import BaseModel import pandas as pd from fastapi.encoders import jsonable_encoder class SomeModel(BaseModel): col1: int col2: str data = [SomeModel(col1=1,col2=”foo”),SomeModel(col1=2,col2=”bar”)]*4*10**5 import cProfile cProfile.run( ‘pd.DataFrame([s.dict() for s in data])’ ) # around 3.4s cProfile.run( ‘pd.DataFrame(jsonable_encoder(data))’ ) # around 20.6s cProfile.run( ‘pd.DataFrame([s.__dict__ for s in data])’ ) … Read more

Passing a function with multiple arguments to DataFrame.apply

It’s just the way you think it would be, apply accepts args and kwargs and passes them directly to some_func. df.apply(some_func, var1=’DOG’, axis=1) Or, df.apply(some_func, args=(‘DOG’, ), axis=1) 0 foo-x-DOG 1 bar-y-DOG dtype: object If for any reason that won’t work for your use case, then you can always fallback to using a lambda: df.apply(lambda … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)