Created
July 7, 2020 11:46
-
-
Save maxcraftsman/5562fedb7496edb1329217c4f470c719 to your computer and use it in GitHub Desktop.
Example of date column replace by function and change depending of the conditions [dirty]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import pandas as pd | |
import shutil | |
import pymysql | |
import sys | |
import re | |
from urllib.request import urlopen | |
from bs4 import BeautifulSoup | |
import sqlalchemy as sa | |
import pytz | |
from datetime import date, time, datetime, timedelta | |
timenow = datetime.now(pytz.timezone('Asia/Tokyo')) | |
timeyesterday = timenow - timedelta(days=1) | |
datenow = timenow.strftime('%d.%m.%Y') | |
dateyesterday = timeyesterday.strftime('%d.%m.%Y') | |
dnstr = datetime.strptime(datenow, '%d.%m.%Y') | |
def time_converter(get_val, pastdate): | |
RU_MONTH_VALUES = { | |
'янв': 1, | |
'фев': 2, | |
'мар': 3, | |
'апр': 4, | |
'мая': 5, | |
'июн': 6, | |
'июл': 7, | |
'авг': 8, | |
'сен': 9, | |
'окт': 10, | |
'ноя': 11, | |
'дек': 12, | |
'сегодня': datenow, | |
'вчера': dateyesterday | |
} | |
def int_value_from_ru_month(date_str): | |
for k, v in RU_MONTH_VALUES.items(): | |
date_str = date_str.replace(k, str(v)) | |
return date_str | |
date_str = get_val | |
date_str = int_value_from_ru_month(date_str) | |
try: | |
d = datetime.strptime(date_str, '%d %m, %H:%M') | |
except: | |
d = datetime.strptime(date_str, '%d.%m.%Y, %H:%M') | |
if (d.year == 1900): | |
if(d.month - dnstr.month > 1): | |
d = d.replace(year=dnstr.year - 1) | |
else: | |
d = d.replace(year=dnstr.year) | |
d = d.replace(day=pastdate) | |
return d.strftime('%d.%m.%Y, %H:%M') | |
def rowIndex(id,date): | |
if id <= 2000: | |
return time_converter(date, 3) | |
if 2000 < id <= 14000: | |
return time_converter(date, 4) | |
if 14000 < id <= 26000: | |
return time_converter(date, 5) | |
if 26000 < id <= 39500: | |
return time_converter(date, 6) | |
if 39500 < id <= 50000: | |
return time_converter(date, 7) | |
return date | |
df['publicDate'] = df.apply(lambda x: rowIndex(x['index'],x['publicDate']),1) | |
df.sort_values(by='index', ascending=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment