Skip to content

Instantly share code, notes, and snippets.

View viniroger's full-sized avatar

Vinicius Roggério da Rocha viniroger

View GitHub Profile
@viniroger
viniroger / convert.py
Created July 22, 2019 13:08
Convert object to datetime64 ns and print columns types
y_true['ds'] = pd.to_datetime(y_true['ds'])
print(y_true.dtypes)
@viniroger
viniroger / filtro.py
Created August 8, 2019 15:17
Ideas for extreme value filter
def filtro(self, df, uid):
"""Filter forecasted values
Check acceptable values, replacing when it isn't
"""
import statistics
if uid == 215:
lower_limit = 10
upper_limit = 3000
elif uid == 216:
lower_limit = 10
@viniroger
viniroger / print_all.py
Last active October 7, 2019 17:48
Print all elements from array
# Print all numpy array, without truncation
import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
# Print all pandas dataframe, without truncation
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
@viniroger
viniroger / calc_mean.py
Created October 15, 2019 11:38
Read CSV with timestamp and calculate 5-min average
def str_to_datetime(s, format_in):
"""
Convert all elements of a pandas series into datetime format
"""
import time
from datetime import datetime
from time import mktime
# Convert from day of year from strptime structure
strptime_object = s.apply(lambda x: time.strptime(x, format_in))
# Create datetime object
@viniroger
viniroger / get_sonda_data.sh
Last active October 18, 2019 13:12
Create URLs and download data from multiples links automatically
#!/bin/bash
# Download files from network
places=('BRB' 'PTR' 'SMS')
years=($(seq 2012 2018))
months=({01..12})
mkdir -p data_amb
for place in "${places[@]}"; do
echo $place
@viniroger
viniroger / fixed_columns.py
Created October 18, 2019 17:37
Save dataframe at file with same width columns
import pandas as pd
df1 = pd.read_csv('input.csv', delimiter=';', header=None)
# Save into fixed columns file
import numpy as np
with open('output.dat', 'w') as ofile:
fmt = '%5d %4d %4d %4d %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f'
np.savetxt(ofile, df1.values, fmt=fmt)
@viniroger
viniroger / replace_value.py
Created October 23, 2019 20:11
Replace value at dataframe without "A value is trying to be set on a copy of a slice from a DataFrame" warning
import pandas as pd
# Loop for all rows
for i in range(0,df.shape[0]):
# Repace value
df.loc[i, 'column_name'] = 5.2
@viniroger
viniroger / print_control.py
Created October 31, 2019 15:10
Print message to follow process evaluation
@staticmethod
def print_control(place, day):
"""
Print dates/place control
"""
#print('%s - %s' %(place, day))
import sys
sys.stdout.write('%s - %s\r' %(place, day))
sys.stdout.flush()
@viniroger
viniroger / sun_minutes.py
Last active December 3, 2019 12:15
Return minutes from sunrise to sunset
# conda install -c conda-forge astral
import astral
@staticmethod
def sun_min(place):
"""
Return minutes from sunrise to sunset
"""
l = astral.Location()
l.latitude = -15.60083333
@viniroger
viniroger / fast_plot.py
Created December 8, 2019 16:51
Plot graphic from CSV
#!/usr/bin/env python3.7.4
# -*- Coding: UTF-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
plt.title('AOD Pequim - Olimpíadas 2008 (221 a 237)')
plt.xlabel('Dia do ano')