无法将 JSON 转换为数据框,Polars 出现了错误。

huangapple go评论94阅读模式
英文:

unable to convert json to dataframe, polars panicked

问题

Here's the translated code portion:

  1. use reqwest::blocking::get;
  2. use polars::prelude::*;
  3. use serde::{Deserialize, Serialize};
  4. use std::io::Cursor;
  5. #[derive(Serialize, Deserialize, Debug)]
  6. struct StockZhAHist {
  7. date: String,
  8. open: f64,
  9. close: f64,
  10. high: f64,
  11. low: f64,
  12. volume: f64,
  13. turnover: f64,
  14. amplitude: f64,
  15. change_rate: f64,
  16. change_amount: f64,
  17. turnover_rate: f64,
  18. }
  19. fn main() -> Result<(), Box<dyn std::error::Error>> {
  20. let url = "http://127.0.0.1:8080/api/public/stock_zh_a_hist";
  21. let response = get(url)?.text()?;
  22. // Deserialize response with serde
  23. let stock_zh_a_hist: Vec<StockZhAHist> = serde_json::from_str(&response)?;
  24. // Convert to polars dataframe
  25. println!("{:?}", stock_zh_a_hist);
  26. let df = JsonReader::new(Cursor::new(response))
  27. .infer_schema_len(Some(1000))
  28. .finish()?;
  29. println!("{:?}", df);
  30. Ok(())
  31. }

Regarding the error you're encountering with Polars, it seems like the Arrow datatype Struct is not supported by Polars. You may need to activate a specific data-type feature to resolve this issue. You mentioned adding the dtype-decimal feature, but it seems it didn't help. You might need to investigate further or seek assistance from the Polars community to determine the correct feature to activate or if there's an alternative approach to handle this specific data type in Polars.

英文:
  1. use reqwest::blocking::get;
  2. use polars::prelude::*;
  3. use serde::{Deserialize, Serialize};
  4. use std::io::Cursor;
  5. #[derive(Serialize, Deserialize, Debug)]
  6. struct StockZhAHist {
  7. date: String,
  8. open: f64,
  9. close: f64,
  10. high: f64,
  11. low: f64,
  12. volume: f64,
  13. turnover: f64,
  14. amplitude: f64,
  15. change_rate: f64,
  16. change_amount: f64,
  17. turnover_rate: f64,
  18. }
  19. fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
  20. let url = &quot;http://127.0.0.1:8080/api/public/stock_zh_a_hist&quot;;
  21. let response = get(url)?.text()?;
  22. // println!(&quot;{}&quot;, response);
  23. // deserialize response with serde
  24. let stock_zh_a_hist: Vec&lt;StockZhAHist&gt; = serde_json::from_str(&amp;response)?;
  25. // convert to polars dataframe
  26. println!(&quot;{:?}&quot;, stock_zh_a_hist);
  27. let df = JsonReader::new(Cursor::new(response))
  28. .infer_schema_len(Some(1000))
  29. .finish()?;
  30. println!(&quot;{:?}&quot;, df);
  31. Ok(())
  32. }

i have written a script that retrieves raw data from my server and i was able to print out the JSON 'stock_zh_a_hist'. but somehow when i tried to print out the dataframe converted from this JSON, the programme panicked and returned :

> thread 'main' panicked at 'Arrow datatype Struct([Field { name:
> "date", data_type: LargeUtf8, is_nullable: true, metadata: {} }, Field
> { name: "open", data_type: Float64, is_nullable: true, metadata: {} },
> Field { name: "close", data_type: Float64, is_nullable: true,
> metadata: {} }, Field { name: "high", data_type: Float64, is_nullable:
> true, metadata: {} }, Field { name: "low", data_type: Float64,
> is_nullable: true, metadata: {} }, Field { name: "volume", data_type:
> Int64, is_nullable: true, metadata: {} }, Field { name: "turnover",
> data_type: Float64, is_nullable: true, metadata: {} }, Field { name:
> "amplitude", data_type: Float64, is_nullable: true, metadata: {} },
> Field { name: "change_rate", data_type: Float64, is_nullable: true,
> metadata: {} }, Field { name: "change_amount", data_type: Float64,
> is_nullable: true, metadata: {} }, Field { name: "turnover_rate",
> data_type: Float64, is_nullable: true, metadata: {} }]) not supported
> by Polars. You probably need to activate that data-type feature.',
> /home/arthur/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-core-0.30.0/src/datatypes/field.rs:158:19.

i also read source code there which reads:

  1. #[cfg(feature = &quot;dtype-decimal&quot;)]
  2. ArrowDataType::Decimal(precision, scale) =&gt; DataType::Decimal(Some(*precision), Some(*scale)),
  3. dt =&gt; panic!(&quot;Arrow datatype {dt:?} not supported by Polars. You probably need to activate that data-type feature.&quot;),

i am assuming polars is having some problem reading the json with correct datatypes, but how can i solve it?

update 1:
i have added the feature dtype-decimal in cargo.toml but the same error persists.

update 2:
the content of stock_zh_a_hist is as follows:

  1. [StockZhAHist { date: &quot;1996-12-16T00:00:00.000&quot;, open: 16.86, close: 16.86, high: 16.86, low: 16.86, volume: 62442.0, turnover: 105277000.0, amplitude: 0.0, change_rate: -10.22, change_amount: -1.92, turnover_rate: 0.87 },
  2. StockZhAHist { date: &quot;1996-12-17T00:00:00.000&quot;, open: 15.17, close: 15.17, high: 16.79, low: 15.17, volume: 463675.0, turnover: 718902016.0, amplitude: 9.61, change_rate: -10.02, change_amount: -1.69, turnover_rate: 6.49 },
  3. StockZhAHist { date: &quot;1996-12-18T00:00:00.000&quot;, open: 15.28, close: 16.69, high: 16.69, low: 15.18, volume: 445380.0, turnover: 719400000.0, amplitude: 9.95, change_rate: 10.02, change_amount: 1.52, turnover_rate: 6.24 },
  4. StockZhAHist { date: &quot;1996-12-19T00:00:00.000&quot;, open: 17.01, close: 16.4, high: 17.9, low: 15.99, volume: 572946.0, turnover: 970124992.0, amplitude: 11.44, change_rate: -1.74, change_amount: -0.29, turnover_rate: 8.03 },
  5. StockZhAHist { date: &quot;1996-12-20T00:00:00.000&quot;, open: 16.19, close: 16.39, high: 16.68, low: 15.9, volume: 277758.0, turnover: 451471008.0, amplitude: 4.76, change_rate: -0.06, change_amount: -0.01, turnover_rate: 3.89 },
  6. StockZhAHist { date: &quot;1996-12-23T00:00:00.000&quot;, open: 16.58, close: 16.57, high: 16.89, low: 16.4, volume: 155369.0, turnover: 258260992.0, amplitude: 2.99, change_rate: 1.1, change_amount: 0.18, turnover_rate: 2.18 },
  7. StockZhAHist { date: &quot;1996-12-24T00:00:00.000&quot;, open: 16.58, close: 15.95, high: 16.73, low: 15.93, volume: 153168.0, turnover: 249324000.0, amplitude: 4.83, change_rate: -3.74, change_amount: -0.62, turnover_rate: 2.15 },
  8. StockZhAHist { date: &quot;1996-12-25T00:00:00.000&quot;, open: 16.1, close: 16.47, high: 16.7, low: 15.86, volume: 175391.0, turnover: 285567008.0, amplitude: 5.27, change_rate: 3.26, change_amount: 0.52, turnover_rate: 2.46 },
  9. StockZhAHist { date: &quot;1996-12-26T00:00:00.000&quot;, open: 16.68, close: 16.47, high: 16.78, low: 16.3, volume: 51516.0, turnover: 85214000.0, amplitude: 2.91, change_rate: 0.0, change_amount: 0.0, turnover_rate: 0.72 }...]

答案1

得分: 2

似乎是一个错误 - 如果您启用特性 dtype-struct,这个问题会消失,但您绝对不应该为了从 JSON 中读取而这样做。

作为一种解决方法,即使没有 dtype-struct,您也可以通过将数据转换为 JSON Lines 来使其工作。

  1. use polars::prelude::{JsonReader, SerReader};
  2. use serde::{Deserialize, Serialize};
  3. use std::io::Cursor;
  4. #[derive(Serialize, Deserialize, Debug)]
  5. struct StockZhAHist {
  6. date: String,
  7. open: f64,
  8. close: f64,
  9. high: f64,
  10. low: f64,
  11. volume: f64,
  12. turnover: f64,
  13. amplitude: f64,
  14. change_rate: f64,
  15. change_amount: f64,
  16. turnover_rate: f64,
  17. }
  18. fn main() -> Result<(), Box<dyn std::error::Error>> {
  19. let data = vec![
  20. StockZhAHist {
  21. date: "1996-12-16T00:00:00.000".into(),
  22. open: 16.86,
  23. close: 16.86,
  24. high: 16.86,
  25. low: 16.86,
  26. volume: 62442.0,
  27. turnover: 105277000.0,
  28. amplitude: 0.0,
  29. change_rate: -10.22,
  30. change_amount: -1.92,
  31. turnover_rate: 0.87,
  32. },
  33. StockZhAHist {
  34. date: "1996-12-17T00:00:00.000".into(),
  35. open: 15.17,
  36. close: 15.17,
  37. high: 16.79,
  38. low: 15.17,
  39. volume: 463675.0,
  40. turnover: 718902016.0,
  41. amplitude: 9.61,
  42. change_rate: -10.02,
  43. change_amount: -1.69,
  44. turnover_rate: 6.49,
  45. },
  46. StockZhAHist {
  47. date: "1996-12-18T00:00:00.000".into(),
  48. open: 15.28,
  49. close: 16.69,
  50. high: 16.69,
  51. low: 15.18,
  52. volume: 445380.0,
  53. turnover: 719400000.0,
  54. amplitude: 9.95,
  55. change_rate: 10.02,
  56. change_amount: 1.52,
  57. turnover_rate: 6.24,
  58. },
  59. ];
  60. // JSON Lines 数据
  61. let response = data
  62. .into_iter()
  63. .map(|d| serde_json::to_string(&d))
  64. .collect::<Result<Vec<_>, _>>()?
  65. .join("\n");
  66. println!("{}", response);
  67. // 转换为 Polars 数据框
  68. let df = JsonReader::new(Cursor::new(response))
  69. .infer_schema_len(Some(1000))
  70. .with_json_format(polars::prelude::JsonFormat::JsonLines)
  71. .finish()?;
  72. println!("{:?}", df);
  73. Ok(())
  74. }
  1. {"date":"1996-12-16T00:00:00.000","open":16.86,"close":16.86,"high":16.86,"low":16.86,"volume":62442.0,"turnover":105277000.0,"amplitude":0.0,"change_rate":-10.22,"change_amount":-1.92,"turnover_rate":0.87}
  2. {"date":"1996-12-17T00:00:00.000","open":15.17,"close":15.17,"high":16.79,"low":15.17,"volume":463675.0,"turnover":718902016.0,"amplitude":9.61,"change_rate":-10.02,"change_amount":-1.69,"turnover_rate":6.49}
  3. {"date":"1996-12-18T00:00:00.000","open":15.28,"close":16.69,"high":16.69,"low":15.18,"volume":445380.0,"turnover":719400000.0,"amplitude":9.95,"change_rate":10.02,"change_amount":1.52,"turnover_rate":6.24}
  4. [/Users/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-io-0.30.0/src/ndjson/core.rs:162] &data_type = Struct(
  5. [
  6. Field {
  7. name: "date",
  8. data_type: LargeUtf8,
  9. is_nullable: true,
  10. metadata: {},
  11. },
  12. Field {
  13. name: "open",
  14. data_type: Float64,
  15. is_nullable: true,
  16. metadata: {},
  17. },
  18. Field {
  19. name: "close",
  20. data_type: Float64,
  21. is_nullable: true,
  22. metadata: {},
  23. },
  24. Field {
  25. name: "high",
  26. data_type: Float64,
  27. is_nullable: true,
  28. metadata: {},
  29. },
  30. Field {
  31. name: "low",
  32. data_type: Float64,
  33. is_nullable: true,
  34. metadata: {},
  35. },
  36. Field {
  37. name: "volume",
  38. data_type: Float64,
  39. is_nullable: true,
  40. metadata: {},
  41. },
  42. Field {
  43. name: "turnover",
  44. data_type: Float64,
  45. is_nullable: true,
  46. metadata: {},
  47. },
  48. Field {
  49. name: "amplitude",
  50. data_type: Float64,
  51. is_nullable: true,
  52. metadata: {},
  53. },
  54. Field {
  55. name: "change_rate",
  56. data_type: Float64,
  57. is_nullable: true,
  58. metadata: {},
  59. },
  60. Field {
  61. name: "change_amount",
  62. data_type: Float64,
  63. is_nullable: true,
  64. metadata: {},
  65. },
  66. Field {
  67. name: "turnover_rate",
  68. data_type: Float64,
  69. is_nullable: true,
  70. metadata: {},
  71. },
  72. ],
  73. )
  74. shape: (3, 11)
  75. ┌─────────────────────────┬───────┬───────┬───────┬───┬───────────┬─────────────┬───────────────┬───────────────┐
  76. date open close high amplitude change_rate change_amount turnover_rate
  77. --- --- --- --- --- --- --- ---
  78. str f64 f64 f64 f64 f64 f64 f64
  79. ╞═════════════════════════╪═══════╪═══════╪═══════╪═══╪════════
  80. <details>
  81. <summary>英文:</summary>
  82. Seems like a bug this issue goes away if you enable feature `dtype-struct`, but you definitely shouldn&#39;t have to in order to read from json.
  83. As a workaround, even without `dtype-struct`, you can get this to work by converting your data to JSON Lines.
  84. ```rust
  85. use polars::prelude::{JsonReader, SerReader};
  86. use serde::{Deserialize, Serialize};
  87. use std::io::Cursor;
  88. #[derive(Serialize, Deserialize, Debug)]
  89. struct StockZhAHist {
  90. date: String,
  91. open: f64,
  92. close: f64,
  93. high: f64,
  94. low: f64,
  95. volume: f64,
  96. turnover: f64,
  97. amplitude: f64,
  98. change_rate: f64,
  99. change_amount: f64,
  100. turnover_rate: f64,
  101. }
  102. fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
  103. let data = vec![
  104. StockZhAHist {
  105. date: &quot;1996-12-16T00:00:00.000&quot;.into(),
  106. open: 16.86,
  107. close: 16.86,
  108. high: 16.86,
  109. low: 16.86,
  110. volume: 62442.0,
  111. turnover: 105277000.0,
  112. amplitude: 0.0,
  113. change_rate: -10.22,
  114. change_amount: -1.92,
  115. turnover_rate: 0.87,
  116. },
  117. StockZhAHist {
  118. date: &quot;1996-12-17T00:00:00.000&quot;.into(),
  119. open: 15.17,
  120. close: 15.17,
  121. high: 16.79,
  122. low: 15.17,
  123. volume: 463675.0,
  124. turnover: 718902016.0,
  125. amplitude: 9.61,
  126. change_rate: -10.02,
  127. change_amount: -1.69,
  128. turnover_rate: 6.49,
  129. },
  130. StockZhAHist {
  131. date: &quot;1996-12-18T00:00:00.000&quot;.into(),
  132. open: 15.28,
  133. close: 16.69,
  134. high: 16.69,
  135. low: 15.18,
  136. volume: 445380.0,
  137. turnover: 719400000.0,
  138. amplitude: 9.95,
  139. change_rate: 10.02,
  140. change_amount: 1.52,
  141. turnover_rate: 6.24,
  142. },
  143. ];
  144. // JSON Lines data
  145. let response = data
  146. .into_iter()
  147. .map(|d| serde_json::to_string(&amp;d))
  148. .collect::&lt;Result&lt;Vec&lt;_&gt;, _&gt;&gt;()?
  149. .join(&quot;\n&quot;);
  150. println!(&quot;{}&quot;, response);
  151. // convert to polars dataframe
  152. let df = JsonReader::new(Cursor::new(response))
  153. .infer_schema_len(Some(1000))
  154. .with_json_format(polars::prelude::JsonFormat::JsonLines)
  155. .finish()?;
  156. println!(&quot;{:?}&quot;, df);
  157. Ok(())
  158. }
  1. {&quot;date&quot;:&quot;1996-12-16T00:00:00.000&quot;,&quot;open&quot;:16.86,&quot;close&quot;:16.86,&quot;high&quot;:16.86,&quot;low&quot;:16.86,&quot;volume&quot;:62442.0,&quot;turnover&quot;:105277000.0,&quot;amplitude&quot;:0.0,&quot;change_rate&quot;:-10.22,&quot;change_amount&quot;:-1.92,&quot;turnover_rate&quot;:0.87}
  2. {&quot;date&quot;:&quot;1996-12-17T00:00:00.000&quot;,&quot;open&quot;:15.17,&quot;close&quot;:15.17,&quot;high&quot;:16.79,&quot;low&quot;:15.17,&quot;volume&quot;:463675.0,&quot;turnover&quot;:718902016.0,&quot;amplitude&quot;:9.61,&quot;change_rate&quot;:-10.02,&quot;change_amount&quot;:-1.69,&quot;turnover_rate&quot;:6.49}
  3. {&quot;date&quot;:&quot;1996-12-18T00:00:00.000&quot;,&quot;open&quot;:15.28,&quot;close&quot;:16.69,&quot;high&quot;:16.69,&quot;low&quot;:15.18,&quot;volume&quot;:445380.0,&quot;turnover&quot;:719400000.0,&quot;amplitude&quot;:9.95,&quot;change_rate&quot;:10.02,&quot;change_amount&quot;:1.52,&quot;turnover_rate&quot;:6.24}
  4. [/Users/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-io-0.30.0/src/ndjson/core.rs:162] &amp;data_type = Struct(
  5. [
  6. Field {
  7. name: &quot;date&quot;,
  8. data_type: LargeUtf8,
  9. is_nullable: true,
  10. metadata: {},
  11. },
  12. Field {
  13. name: &quot;open&quot;,
  14. data_type: Float64,
  15. is_nullable: true,
  16. metadata: {},
  17. },
  18. Field {
  19. name: &quot;close&quot;,
  20. data_type: Float64,
  21. is_nullable: true,
  22. metadata: {},
  23. },
  24. Field {
  25. name: &quot;high&quot;,
  26. data_type: Float64,
  27. is_nullable: true,
  28. metadata: {},
  29. },
  30. Field {
  31. name: &quot;low&quot;,
  32. data_type: Float64,
  33. is_nullable: true,
  34. metadata: {},
  35. },
  36. Field {
  37. name: &quot;volume&quot;,
  38. data_type: Float64,
  39. is_nullable: true,
  40. metadata: {},
  41. },
  42. Field {
  43. name: &quot;turnover&quot;,
  44. data_type: Float64,
  45. is_nullable: true,
  46. metadata: {},
  47. },
  48. Field {
  49. name: &quot;amplitude&quot;,
  50. data_type: Float64,
  51. is_nullable: true,
  52. metadata: {},
  53. },
  54. Field {
  55. name: &quot;change_rate&quot;,
  56. data_type: Float64,
  57. is_nullable: true,
  58. metadata: {},
  59. },
  60. Field {
  61. name: &quot;change_amount&quot;,
  62. data_type: Float64,
  63. is_nullable: true,
  64. metadata: {},
  65. },
  66. Field {
  67. name: &quot;turnover_rate&quot;,
  68. data_type: Float64,
  69. is_nullable: true,
  70. metadata: {},
  71. },
  72. ],
  73. )
  74. shape: (3, 11)
  75. ┌─────────────────────────┬───────┬───────┬───────┬───┬───────────┬─────────────┬───────────────┬───────────────┐
  76. date open close high amplitude change_rate change_amount turnover_rate
  77. --- --- --- --- --- --- --- ---
  78. str f64 f64 f64 f64 f64 f64 f64
  79. ╞═════════════════════════╪═══════╪═══════╪═══════╪═══╪═══════════╪═════════════╪═══════════════╪═══════════════╡
  80. 1996-12-16T00:00:00.000 16.86 16.86 16.86 0.0 -10.22 -1.92 0.87
  81. 1996-12-17T00:00:00.000 15.17 15.17 16.79 9.61 -10.02 -1.69 6.49
  82. 1996-12-18T00:00:00.000 15.28 16.69 16.69 9.95 10.02 1.52 6.24
  83. └─────────────────────────┴───────┴───────┴───────┴───┴───────────┴─────────────┴───────────────┴───────────────┘

huangapple
  • 本文由 发表于 2023年6月27日 17:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定