出现以下错误,请给出修改后完整的代码NameError Traceback (most recent call last)
Cell In[23], line 181
178 print("未能获取到任何股票数据")
180 if __name__ == "__main__":
--> 181 main()
Cell In[23], line 142, in main()
139 financial_data = get_financial_data(stock_code, stock_name)
141 # 合并数据
--> 142 merged_data = merge_data(price_data, financial_data)
144 if merged_data is not None:
145 all_data.append(merged_data)
Cell In[23], line 103, in merge_data(price_data, financial_data)
99 merged_data = price_data.copy()
101 if financial_data is not None and not financial_data.empty:
102 # 找到最近的财务报告日期
--> 103 merged_data['nearest_report_date'] = merged_data['date'].apply(
104 lambda x: find_nearest_report_date(x, financial_data['report_date'])
105 )
107 # 合并行情和财务数据
108 financial_data = financial_data.rename(columns={'report_date': 'nearest_report_date'})
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/series.py:4935, in Series.apply(self, func, convert_dtype, args, by_row, **kwargs)
4800 def apply(
4801 self,
4802 func: AggFuncType,
(...) 4807 **kwargs,
4808 ) -> DataFrame | Series:
4809 """
4810 Invoke function on values of Series.
4811
(...) 4926 dtype: float64
4927 """
4928 return SeriesApply(
4929 self,
4930 func,
4931 convert_dtype=convert_dtype,
4932 by_row=by_row,
4933 args=args,
4934 kwargs=kwargs,
-> 4935 ).apply()
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/apply.py:1422, in SeriesApply.apply(self)
1419 return self.apply_compat()
1421 # self.func is Callable
-> 1422 return self.apply_standard()
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/apply.py:1502, in SeriesApply.apply_standard(self)
1496 # row-wise access
1497 # apply doesn't have a `na_action` keyword and for backward compat reasons
1498 # we need to give `na_action="ignore"` for categorical data.
1499 # TODO: remove the `na_action="ignore"` when that default has been changed in
1500 # Categorical (GH51645).
1501 action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
-> 1502 mapped = obj._map_values(
1503 mapper=curried, na_action=action, convert=self.convert_dtype
1504 )
1506 if len(mapped) and isinstance(mapped[0], ABCSeries):
1507 # GH#43986 Need to do list(mapped) in order to get treated as nested
1508 # See also GH#25959 regarding EA support
1509 return obj._constructor_expanddim(list(mapped), index=obj.index)
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/base.py:923, in IndexOpsMixin._map_values(self, mapper, na_action, convert)
920 arr = self._values
922 if isinstance(arr, ExtensionArray):
--> 923 return arr.map(mapper, na_action=na_action)
925 return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/arrays/_mixins.py:81, in ravel_compat.<locals>.method(self, *args, **kwargs)
78 @wraps(meth)
79 def method(self, *args, **kwargs):
80 if self.ndim == 1:
---> 81 return meth(self, *args, **kwargs)
83 flags = self._ndarray.flags
84 flat = self.ravel("K")
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/arrays/datetimelike.py:763, in DatetimeLikeArrayMixin.map(self, mapper, na_action)
759 @ravel_compat
760 def map(self, mapper, na_action=None):
761 from pandas import Index
--> 763 result = map_array(self, mapper, na_action=na_action)
764 result = Index(result)
766 if isinstance(result, ABCMultiIndex):
File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/algorithms.py:1743, in map_array(arr, mapper, na_action, convert)
1741 values = arr.astype(object, copy=False)
1742 if na_action is None:
-> 1743 return lib.map_infer(values, mapper, convert=convert)
1744 else:
1745 return lib.map_infer_mask(
1746 values, mapper, mask=isna(values).view(np.uint8), convert=convert
1747 )
File pandas/_libs/lib.pyx:2999, in pandas._libs.lib.map_infer()
Cell In[23], line 104, in merge_data.<locals>.<lambda>(x)
99 merged_data = price_data.copy()
101 if financial_data is not None and not financial_data.empty:
102 # 找到最近的财务报告日期
103 merged_data['nearest_report_date'] = merged_data['date'].apply(
--> 104 lambda x: find_nearest_report_date(x, financial_data['report_date'])
105 )
107 # 合并行情和财务数据
108 financial_data = financial_data.rename(columns={'report_date': 'nearest_report_date'})
Cell In[23], line 122, in find_nearest_report_date(target_date, report_dates)
120 return None
121 valid_dates = report_dates[report_dates <= target_date]
--> 122 return valid_dates.max() if not valid_empty_dates else report_dates.min()
NameError: name 'valid_empty_dates' is not defined