Advertisement
furas

Python - pandas - apply - (Stackoverflow)

Feb 22nd, 2025 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. # date: 2025.02.22
  2. # author: furas
  3.  
  4. # stackoverflow: [pandas - python definition that use try and except loop to create a column using the apply funtion - Stack Overflow](https://stackoverflow.com/questions/79446546/python-definition-that-use-try-and-except-loop-to-create-a-column-using-the-appl)
  5.  
  6. # doc: [pandas.DataFrame.apply — pandas 2.2.3 documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html)
  7.  
  8. # --- version 1 - without `apply` ---
  9.  
  10. def date_difference(dataframe):
  11.     dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
  12.     dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
  13.     dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
  14.     dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
  15.  
  16.     results = []
  17.  
  18.     for x, y in zip(dataframe['Issued Date'], dataframe['Permit Creation Date']):
  19.         try:
  20.             d1 = datetime.datetime.strptime(x, "%Y-%m-%d")
  21.             d2 = datetime.datetime.strptime(y, "%Y-%m-%d")
  22.             print(abs(d1-d2).days)
  23.             results.append(abs(d1-d2).days)
  24.         except Exception as e:
  25.             print(e.args)
  26.             results.append(None)
  27.  
  28.     return results
  29.  
  30. type1['Date difference'] = date_difference(type1)  # without `apply`
  31.  
  32. # --- version 2 - without `apply` - some modifications outside function ---
  33.  
  34. dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
  35. dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
  36.  
  37. dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
  38. dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
  39.  
  40. def date_difference(dataframe):
  41.  
  42.     results = []
  43.  
  44.     for x, y in zip(dataframe['Issued Date'], dataframe['Permit Creation Date']):
  45.         try:
  46.             d1 = datetime.datetime.strptime(x, "%Y-%m-%d")
  47.             d2 = datetime.datetime.strptime(y, "%Y-%m-%d")
  48.             print(abs(d1-d2).days)
  49.             results.append(abs(d1-d2).days)
  50.         except Exception as e:
  51.             print(e.args)
  52.             results.append(None)
  53.  
  54.     return results
  55.  
  56. type1['Date difference'] = date_difference(type1)  # without `apply`
  57.  
  58. # --- version 3 - without `apply` - some modifications outside function - use `iterrows()` instead of `zip()` ---
  59.  
  60. dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
  61. dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
  62.  
  63. dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
  64. dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
  65.  
  66. def date_difference(dataframe):
  67.  
  68.     results = []
  69.  
  70.     for index, row in dataframe.iterrows():  # use `iterrows()`
  71.         try:
  72.             d1 = datetime.datetime.strptime(row['Issued Date'], "%Y-%m-%d")
  73.             d2 = datetime.datetime.strptime(row['Permit Creation Date'], "%Y-%m-%d")
  74.             print(abs(d1-d2).days)
  75.             results.append(abs(d1-d2).days)
  76.         except Exception as e:
  77.             print(e.args)
  78.             results.append(None)
  79.  
  80.     return results
  81.  
  82. type1['Date difference'] = date_difference(type1)  # without `apply`
  83.  
  84. # --- version 4 - apply ---
  85.  
  86. dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
  87. dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
  88.  
  89. dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
  90. dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
  91.  
  92. def apply_date_difference(row):  # it gets single row (Series)
  93.     try:
  94.         d1 = datetime.datetime.strptime(row['Issued Date'], "%Y-%m-%d")
  95.         d2 = datetime.datetime.strptime(row['Permit Creation Date'], "%Y-%m-%d")
  96.         print(abs(d1-d2).days)
  97.         return abs(d1-d2).days
  98.     except Exception as e:
  99.         print(e.args)
  100.         return None
  101.  
  102. type1['Date difference'] = type1.apply(date_difference)
  103.  
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement