tbdavid2019 commited on
Commit
426f91f
·
1 Parent(s): ead29e0
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -19,28 +19,46 @@ def prepare_data_chronos(data):
19
  formatted_df = pd.DataFrame({
20
  'item_id': ['stock'] * len(df),
21
  'timestamp': pd.to_datetime(df['Date']),
22
- 'value': df['Close'].astype('float32') # 使用 'value' 而不是 'target'
23
  })
24
 
25
- print("Data types:", formatted_df.dtypes)
26
- print("Sample data:", formatted_df.head())
27
  # 按照時間戳排序
28
  formatted_df = formatted_df.sort_values('timestamp')
29
 
30
  try:
31
- # 使用 from_data_frame 方法創建 TimeSeriesDataFrame
32
- ts_df = TimeSeriesDataFrame.from_data_frame(
33
- formatted_df,
34
- id_column='item_id',
35
- timestamp_column='timestamp',
36
- target_column='value'
37
- )
38
  return ts_df
39
  except Exception as e:
40
  print(f"Error creating TimeSeriesDataFrame: {str(e)}")
 
 
41
  raise
42
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  # Function to fetch stock indices (you already defined these)
45
  def get_tw0050_stocks():
46
  response = requests.get('https://answerbook.david888.com/TW0050')
 
19
  formatted_df = pd.DataFrame({
20
  'item_id': ['stock'] * len(df),
21
  'timestamp': pd.to_datetime(df['Date']),
22
+ 'value': df['Close'].astype('float32').values.ravel() # 確保是 1-dimensional
23
  })
24
 
 
 
25
  # 按照時間戳排序
26
  formatted_df = formatted_df.sort_values('timestamp')
27
 
28
  try:
29
+ # 直接創建 TimeSeriesDataFrame
30
+ ts_df = TimeSeriesDataFrame(formatted_df)
31
+
32
+ # 打印一些診斷信息
33
+ print("TimeSeriesDataFrame shape:", ts_df.shape)
34
+ print("Value column shape:", ts_df['value'].shape)
35
+
36
  return ts_df
37
  except Exception as e:
38
  print(f"Error creating TimeSeriesDataFrame: {str(e)}")
39
+ print("Formatted DataFrame info:")
40
+ print(formatted_df.info())
41
  raise
42
 
43
 
44
+ # def prepare_data_chronos(data):
45
+ # # 直接使用收盤價序列
46
+ # series = pd.Series(
47
+ # data['Close'].values,
48
+ # index=data.index,
49
+ # name='value'
50
+ # )
51
+
52
+ # # 創建基本的時間序列數據框
53
+ # df = pd.DataFrame({
54
+ # 'timestamp': series.index,
55
+ # 'value': series.values,
56
+ # 'item_id': ['stock'] * len(series)
57
+ # })
58
+
59
+ # return TimeSeriesDataFrame(df)
60
+
61
+
62
  # Function to fetch stock indices (you already defined these)
63
  def get_tw0050_stocks():
64
  response = requests.get('https://answerbook.david888.com/TW0050')