예제 #1
0
    def _loadCSVDataFrame(self):
        """Loads the given csv file with pandas and generate a new dataframe.

        The file will be loaded with the configured encoding, delimiter
        and header.git
        If any execptions will occur, an empty Dataframe is generated
        and a message will appear in the status bar.

        Returns:
            pandas.DataFrame: A dataframe containing all the available
                information of the csv file.

        """
        if self._filename and os.path.exists(self._filename):
            # default fallback if no encoding was found/selected
            encoding = self._encodingKey or 'UTF_8'

            try:
                dataFrame = superReadFile(self._filename,
                                          sep=self._delimiter,
                                          first_codec=encoding,
                                          header=self._header)
                dataFrame = dataFrame.apply(fillNoneValues)
                dataFrame = dataFrame.apply(convertTimestamps)
            except Exception as err:
                self.updateStatusBar(str(err))
                print(err)
                return pandas.DataFrame()
            self.updateStatusBar('Preview generated.')
            return dataFrame
        self.updateStatusBar('File could not be read.')
        return pandas.DataFrame()
예제 #2
0
 def import_template(self, filename=None, columns=None):
     def_cols = ['old', 'new', 'dtype']
     if filename is None:
         filename = QtGui.QFileDialog.getOpenFileName()[0]
     df = superReadFile(filename)
     if columns is None:
         df = df.loc[:, def_cols]
         columns = def_cols
     self.apply_template(df, columns=columns)
예제 #3
0
def read_file(filepath, **kwargs):
    """
    Read a data file into a DataFrameModel.

    :param filepath: The rows/columns filepath to read.
    :param kwargs:
            xls/x files - see pandas.read_excel(**kwargs)
            .csv/.txt/etc - see pandas.read_csv(**kwargs)
    :return: DataFrameModel
    """
    return DataFrameModel(dataFrame=superReadFile(filepath, **kwargs),
                          filePath=filepath)
예제 #4
0
    def setDataFrameFromFile(self, filepath, **kwargs):
        """
        Sets the model's dataFrame by reading a file.
        Accepted file formats:
            - .xlsx (sheet1 is read unless specified in kwargs)
            - .csv (comma separated unless specified in kwargs)
            - .txt (any separator)

        :param filepath: (str)
            The path to the file to be read.
        :param kwargs:
            pandas.read_csv(**kwargs) or pandas.read_excel(**kwargs)
        :return: None
        """
        df = superReadFile(filepath, **kwargs)
        self.setDataFrame(df, filePath=filepath)