Sha256: f21025d67d211579a7a2495e653c27d96f07b3da5d71e8e09eeb37276a475483

Contents?: true

Size: 1.34 KB

Versions: 96

Compression:

Stored size: 1.34 KB

Contents

module EasyML
  module Data
    module Utils
      def append_to_csv(df, path)
        return if df.empty?

        path = Pathname.new(path) if path.is_a?(String)
        FileUtils.mkdir_p(path.dirname)
        FileUtils.touch(path)

        # Check if the file is empty (i.e., if this is the first write)
        file_empty = File.zero?(path)

        # Write the DataFrame to a temporary file
        temp_file = "#{path}.tmp"
        df.write_csv(temp_file)

        # Append the content to the main file, skipping the header if not the first write
        File.open(path, "a") do |f|
          File.foreach(temp_file).with_index do |line, index|
            # Skip the header line if the file is not empty
            f.write(line) unless index == 0 && !file_empty
          end
        end

        # Delete the temporary file
        File.delete(temp_file)
      end

      def expand_dir(dir)
        return dir if dir.to_s[0] == "/"

        Rails.root.join(dir)
      end

      def null_check(df)
        result = {}
        null_counts = df.null_count
        total_count = df.height
        df.columns.each do |column|
          null_count = null_counts[column][0]
          next if null_count == 0

          result[column] = { null_count: null_count, total_count: total_count }
        end
        result.empty? ? nil : result
      end
    end
  end
end

Version data entries

96 entries across 96 versions & 1 rubygems

Version Path
easy_ml-0.2.0.pre.rc105 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc104 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc103 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc102 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc101 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc100 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc99 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc98 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc97 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc96 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc95 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc94 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc93 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc92 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc91 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc90 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc89 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc88 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc85 lib/easy_ml/data/utils.rb
easy_ml-0.2.0.pre.rc84 lib/easy_ml/data/utils.rb