MCPcopy
hub / github.com/pandas-dev/pandas / _handle_usecols

Method _handle_usecols

pandas/io/parsers/python_parser.py:767–813  ·  view source on GitHub ↗

Sets self._col_indices usecols_key is used if there are string usecols.

(
        self,
        columns: list[list[Scalar | None]],
        usecols_key: list[Scalar | None],
        num_original_columns: int,
    )

Source from the content-addressed store, hash-verified

765 return line
766
767 def _handle_usecols(
768 self,
769 columns: list[list[Scalar | None]],
770 usecols_key: list[Scalar | None],
771 num_original_columns: int,
772 ) -> list[list[Scalar | None]]:
773 """
774 Sets self._col_indices
775
776 usecols_key is used if there are string usecols.
777 """
778 col_indices: set[int] | list[int]
779 if self.usecols is not None:
780 if callable(self.usecols):
781 col_indices = evaluate_callable_usecols(self.usecols, usecols_key)
782 elif any(isinstance(u, str) for u in self.usecols):
783 if len(columns) > 1:
784 raise ValueError(
785 "If using multiple headers, usecols must be integers."
786 )
787 col_indices = []
788
789 for col in self.usecols:
790 if isinstance(col, str):
791 try:
792 col_indices.append(usecols_key.index(col))
793 except ValueError:
794 self._validate_usecols_names(self.usecols, usecols_key)
795 else:
796 col_indices.append(col)
797 else:
798 missing_usecols = [
799 col for col in self.usecols if col >= num_original_columns
800 ]
801 if missing_usecols:
802 raise ParserError(
803 "Defining usecols with out-of-bounds indices is not allowed. "
804 f"{missing_usecols} are out-of-bounds.",
805 )
806 col_indices = self.usecols
807
808 columns = [
809 [n for i, n in enumerate(column) if i in col_indices]
810 for column in columns
811 ]
812 self._col_indices = sorted(col_indices)
813 return columns
814
815 def _buffered_line(self) -> list[Scalar]:
816 """

Callers 1

_infer_columnsMethod · 0.95

Calls 5

ParserErrorClass · 0.90
appendMethod · 0.45
indexMethod · 0.45

Tested by

no test coverage detected