Repository: activeloopai/Hub
File: deeplake/auto/structured/dataframe.py (Python)

Code before:
    #         str: A string with the sanitized tensor name
    #     """

    #     invalid_chars = ["[", "]", "@", ".", ",", "?", "!", "/", "\\", "#", "'", '"']
    #     new_char = "_"
    #     for char in invalid_chars:
    #         input = input.replace(char, new_char)

    #     return input

    def _initialize_params(self, column_params):
        column_params = column_params if column_params is not None else {}

        keys = list(self.source.columns)

        for key in keys:
            if key in column_params.keys():
                column_params[key]["name"] = column_params[key].get(
                    "name", sanitize_tensor_name(key)
                )  # Get the specified name, and if it doesn't exist, use the sanitized column name
            else:
                column_params[key] = {
                    "name": sanitize_tensor_name(key)
                }  # If column parameters are not specified for a column, use the sanitized column name

        self.column_params = column_params

    def _get_most_frequent_image_extension(self, fn_iterator):
        # TODO: Make this generic and work for any htype that requires compression

        supported_image_extensions = tuple(
            HTYPE_SUPPORTED_COMPRESSIONS["image"] + ["jpg"]
        )
        image_extensions: DefaultDict[str, int] = defaultdict(int)

        for file in fn_iterator:
            if file.endswith(supported_image_extensions):
                ext = pathlib.Path(file).suffix[
                    1:
                ]  # Get extension without the . symbol
                image_extensions[ext] += 1
            else:
                raise IngestionError(f"The following file is not supported: {file}")

        most_frequent_image_extension = max(
            image_extensions, key=lambda k: image_extensions[k], default=None
        )

        return most_frequent_image_extension

    def _parse_tensor_params(self, key, inspect_limit=1000):

Review comment (security):
Consider following refactor, call sanitize method only if necessary:

```suggestion
        column_params = column_params or {}
        for key in self.source.columns:
            params = column_params.get(key)
            if params:
                if "name" not in params:
                    params["name"] = sanitize_tensor_name(key)
            else:
                 column_params[key] = {"name": sanitize_tensor_name(key)}
        self.column_params = column_params
```

Code after:
            else:
                column_params[key] = {"name": sanitize_tensor_name(key)}
        self.column_params = column_params

    def _get_most_frequent_image_extension(self, fn_iterator):
        # TODO: Make this generic and work for any htype that requires compression
        supported_image_extensions = tuple(
            "." + fmt for fmt in HTYPE_SUPPORTED_COMPRESSIONS["image"] + ["jpg"]
        )
        image_extensions: DefaultDict[str, int] = defaultdict(int)
        for file in fn_iterator:
            if file.lower().endswith(supported_image_extensions):
                ext = file.rsplit(".", 1)[1]
                image_extensions[ext] += 1
            else:
                raise IngestionError(f"The following file is not supported: {file}")

        most_frequent_image_extension = max(
            image_extensions, key=lambda k: image_extensions[k], default=None
        )
        return most_frequent_image_extension

    def _parse_tensor_params(self, key, inspect_limit=1000):
        """Parse the tensor parameters for a column. Required parameters that are not specified will be inferred by inspecting up to 'inspect_limit' rows in the data."""

        tensor_params: Dict = self.column_params[key]

        dtype = self.source[key].dtype
        if (
            "htype" not in tensor_params
        ):  # Auto-set some typing parameters if htype is not specified
            if dtype == np.dtype("object"):
                types = [type(v) for v in self.source[key][0:inspect_limit].values]

                if len(set(types)) != 1:
                    raise IngestionError(
                        f"Dataframe has different data types inside '{key}' column. Please make sure all data is given column is compatible with a single Deep Lake htype, or try specifying the htype manually."
                    )

                if types[0] == str:
                    tensor_params.update(
                        htype="text"
                    )  # Use "text" htype for text data when the htype is not specified tensor_params
            else:
                tensor_params.update(
                    dtype=dtype,
                    create_shape_tensor=tensor_params.get("create_shape_tensor", False),
                )  # htype will be auto-inferred for numeric data unless the htype is specified in tensor_params

        # TODO: Make this more robust so it works for all htypes where sample_compression is required and should be inferred from the data itself
        if (