# Buggy code
def __init__(
    self,
    obj,
    path_or_buf: Optional[FilePathOrBuffer[str]] = None,
    sep: str = ",",
    na_rep: str = "",
    float_format: Optional[str] = None,
    cols=None,
    header: Union[bool, Sequence[Hashable]] = True,
    index: bool = True,
    index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None,
    mode: str = "w",
    encoding: Optional[str] = None,
    compression: Union[str, Mapping[str, str], None] = "infer",
    quoting: Optional[int] = None,
    line_terminator="\n",
    chunksize: Optional[int] = None,
    quotechar='"',
    date_format: Optional[str] = None,
    doublequote: bool = True,
    escapechar: Optional[str] = None,
    decimal=".",
):
    self.obj = obj

    if path_or_buf is None:
        path_or_buf = StringIO()

    # Extract compression mode as given, if dict
    compression, self.compression_args = get_compression_method(compression)

    self.path_or_buf, _, _, _ = get_filepath_or_buffer(
        path_or_buf, encoding=encoding, compression=compression, mode=mode
    )
    self.sep = sep
    self.na_rep = na_rep
    self.float_format = float_format
    self.decimal = decimal

    self.header = header
    self.index = index
    self.index_label = index_label
    self.mode = mode
    if encoding is None:
        encoding = "utf-8"
    self.encoding = encoding
    self.compression = infer_compression(self.path_or_buf, compression)

    if quoting is None:
        quoting = csvlib.QUOTE_MINIMAL
    self.quoting = quoting

    if quoting == csvlib.QUOTE_NONE:
        # prevents crash in _csv
        quotechar = None
    self.quotechar = quotechar

    self.doublequote = doublequote
    self.escapechar = escapechar

    self.line_terminator = line_terminator or os.linesep

    self.date_format = date_format

    self.has_mi_columns = isinstance(obj.columns, ABCMultiIndex)

    # validate mi options
    if self.has_mi_columns:
        if cols is not None:
            raise TypeError("cannot specify cols with a MultiIndex on the columns")

    if cols is not None:
        if isinstance(cols, ABCIndexClass):
            cols = cols.to_native_types(
                na_rep=na_rep,
                float_format=float_format,
                date_format=date_format,
                quoting=self.quoting,
            )
        else:
            cols = list(cols)
        self.obj = self.obj.loc[:, cols]

    # update columns to include possible multiplicity of dupes
    # and make sure sure cols is just a list of labels
    cols = self.obj.columns
    if isinstance(cols, ABCIndexClass):
        cols = cols.to_native_types(
            na_rep=na_rep,
            float_format=float_format,
            date_format=date_format,
            quoting=self.quoting,
        )
    else:
        cols = list(cols)

    # save it
    self.cols = cols

    # preallocate data 2d list
    ncols = self.obj.shape[-1]
    self.data = [None] * ncols

    if chunksize is None:
        chunksize = (100000 // (len(self.cols) or 1)) or 1
    self.chunksize = int(chunksize)

    self.data_index = obj.index
    if (
        isinstance(self.data_index, (ABCDatetimeIndex, ABCPeriodIndex))
        and date_format is not None
    ):
        from pandas import Index

        self.data_index = Index(
            [x.strftime(date_format) if notna(x) else "" for x in self.data_index]
        )

    self.nlevels = getattr(self.data_index, "nlevels", 1)
    if not index:
        self.nlevels = 0


# Traceback (botocore.exceptions.NoCredentialsError)
Exception ignored in: <function AbstractBufferedFile.__del__ at 0x7fe0ae8db440>
Traceback (most recent call last):
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/fsspec/spec.py", line 1137, in __del__
self.close()
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/fsspec/spec.py", line 1114, in close
self.flush(force=True)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/fsspec/spec.py", line 986, in flush
self._initiate_upload()
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/s3fs/core.py", line 951, in _initiate_upload
Bucket=self.bucket, Key=self.key, ACL=self.acl)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/s3fs/core.py", line 939, in _call_s3
**kwargs)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/s3fs/core.py", line 182, in _call_s3
return method(**additional_kwargs)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/client.py", line 648, in _make_api_call
operation_model, request_dict, request_context)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/client.py", line 667, in _make_request
return self._endpoint.make_request(operation_model, request_dict)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/endpoint.py", line 102, in make_request
return self._send_request(request_dict, operation_model)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/endpoint.py", line 132, in _send_request
request = self.create_request(request_dict, operation_model)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/endpoint.py", line 116, in create_request
operation_name=operation_model.name)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/signers.py", line 90, in handler
return self.sign(operation_name, request)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/signers.py", line 157, in sign
auth.add_auth(request)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/auth.py", line 425, in add_auth
super(S3SigV4Auth, self).add_auth(request)
File "/home/languitar/.pyenv/versions/analytics-3.7/lib/python3.7/site-packages/botocore/auth.py", line 357, in add_auth
raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

# Fixed code
def __init__(
    self,
    obj,
    path_or_buf: Optional[FilePathOrBuffer[str]] = None,
    sep: str = ",",
    na_rep: str = "",
    float_format: Optional[str] = None,
    cols=None,
    header: Union[bool, Sequence[Hashable]] = True,
    index: bool = True,
    index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None,
    mode: str = "w",
    encoding: Optional[str] = None,
    compression: Union[str, Mapping[str, str], None] = "infer",
    quoting: Optional[int] = None,
    line_terminator="\n",
    chunksize: Optional[int] = None,
    quotechar='"',
    date_format: Optional[str] = None,
    doublequote: bool = True,
    escapechar: Optional[str] = None,
    decimal=".",
):
    self.obj = obj

    if path_or_buf is None:
        path_or_buf = StringIO()

    # Extract compression mode as given, if dict
    compression, self.compression_args = get_compression_method(compression)

    self.path_or_buf, _, _, self.should_close = get_filepath_or_buffer(
        path_or_buf, encoding=encoding, compression=compression, mode=mode
    )
    self.sep = sep
    self.na_rep = na_rep
    self.float_format = float_format
    self.decimal = decimal

    self.header = header
    self.index = index
    self.index_label = index_label
    self.mode = mode
    if encoding is None:
        encoding = "utf-8"
    self.encoding = encoding
    self.compression = infer_compression(self.path_or_buf, compression)

    if quoting is None:
        quoting = csvlib.QUOTE_MINIMAL
    self.quoting = quoting

    if quoting == csvlib.QUOTE_NONE:
        # prevents crash in _csv
        quotechar = None
    self.quotechar = quotechar

    self.doublequote = doublequote
    self.escapechar = escapechar

    self.line_terminator = line_terminator or os.linesep

    self.date_format = date_format

    self.has_mi_columns = isinstance(obj.columns, ABCMultiIndex)

    # validate mi options
    if self.has_mi_columns:
        if cols is not None:
            raise TypeError("cannot specify cols with a MultiIndex on the columns")

    if cols is not None:
        if isinstance(cols, ABCIndexClass):
            cols = cols.to_native_types(
                na_rep=na_rep,
                float_format=float_format,
                date_format=date_format,
                quoting=self.quoting,
            )
        else:
            cols = list(cols)
        self.obj = self.obj.loc[:, cols]

    # update columns to include possible multiplicity of dupes
    # and make sure sure cols is just a list of labels
    cols = self.obj.columns
    if isinstance(cols, ABCIndexClass):
        cols = cols.to_native_types(
            na_rep=na_rep,
            float_format=float_format,
            date_format=date_format,
            quoting=self.quoting,
        )
    else:
        cols = list(cols)

    # save it
    self.cols = cols

    # preallocate data 2d list
    ncols = self.obj.shape[-1]
    self.data = [None] * ncols

    if chunksize is None:
        chunksize = (100000 // (len(self.cols) or 1)) or 1
    self.chunksize = int(chunksize)

    self.data_index = obj.index
    if (
        isinstance(self.data_index, (ABCDatetimeIndex, ABCPeriodIndex))
        and date_format is not None
    ):
        from pandas import Index

        self.data_index = Index(
            [x.strftime(date_format) if notna(x) else "" for x in self.data_index]
        )

    self.nlevels = getattr(self.data_index, "nlevels", 1)
    if not index:
        self.nlevels = 0