|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import os |
4 | | -import ast |
5 | | -import json |
6 | 4 | import inspect |
7 | 5 | import logging |
8 | 6 | import datetime |
|
25 | 23 | import anyio |
26 | 24 | import httpx |
27 | 25 | import pydantic |
28 | | -from pandas import DataFrame # type: ignore[import] |
29 | 26 |
|
30 | 27 | from ._types import NoneType |
31 | 28 | from ._utils import is_given, extract_type_arg, is_annotated_type, is_type_alias_type, extract_type_var_from_base |
@@ -482,61 +479,6 @@ class BinaryAPIResponse(APIResponse[bytes]): |
482 | 479 | the API request, e.g. `.with_streaming_response.get_binary_response()` |
483 | 480 | """ |
484 | 481 |
|
485 | | - def to_dataframe(self) -> DataFrame: |
486 | | - """Convert the response data to a pandas DataFrame. |
487 | | -
|
488 | | - Note: This method requires the `pandas` library to be installed. |
489 | | -
|
490 | | - Returns: |
491 | | - DataFrame: Processed evaluation data |
492 | | - """ |
493 | | - # Read the binary content |
494 | | - content = self.read() |
495 | | - |
496 | | - # Now decode the content |
497 | | - lines = content.decode("utf-8").strip().split("\n") |
498 | | - |
499 | | - # Parse each line and flatten the results |
500 | | - data = [] |
501 | | - for line in lines: |
502 | | - try: |
503 | | - entry = json.loads(line) |
504 | | - # Parse the results field directly from JSON |
505 | | - if 'results' in entry: |
506 | | - if isinstance(entry['results'], str): |
507 | | - # Try to handle string representations that are valid JSON |
508 | | - try: |
509 | | - results = json.loads(entry['results']) |
510 | | - except Exception as e: |
511 | | - # If not valid JSON, fall back to safer processing |
512 | | - results = ast.literal_eval(entry['results']) |
513 | | - else: |
514 | | - # Already a dictionary |
515 | | - results = entry['results'] |
516 | | - |
517 | | - # Remove the original results field |
518 | | - del entry['results'] |
519 | | - # Flatten the nested dictionary structure |
520 | | - if isinstance(results, dict): |
521 | | - for key, value in results.items(): # type: ignore |
522 | | - if isinstance(value, dict): |
523 | | - for subkey, subvalue in value.items(): # type: ignore |
524 | | - if isinstance(subvalue, dict): |
525 | | - # Handle one more level of nesting |
526 | | - for subsubkey, subsubvalue in subvalue.items(): # type: ignore |
527 | | - entry[f'{key}_{subkey}_{subsubkey}'] = subsubvalue |
528 | | - else: |
529 | | - entry[f'{key}_{subkey}'] = subvalue |
530 | | - else: |
531 | | - entry[key] = value |
532 | | - |
533 | | - data.append(entry) # type: ignore |
534 | | - except Exception as e: |
535 | | - log.error(f"Error processing line: {e}") |
536 | | - log.error(f"Problematic line: {line[:200]}...") # Print first 200 chars of the line |
537 | | - continue |
538 | | - return DataFrame(data) |
539 | | - |
540 | 482 | def write_to_file( |
541 | 483 | self, |
542 | 484 | file: str | os.PathLike[str], |
|
0 commit comments