<jupyter_start><jupyter_text>Incase if some columns doesn't have names<jupyter_code>a_file = open(filename1, "r+")
list_of_lines = a_file.readlines()

if ('a,b' in list_of_lines[0]) == False:
    list_of_lines[0] = list_of_lines[0][:-1] + ',a,b\n'

a_file = open(filename1, "w")
a_file.writelines(list_of_lines)
a_file.close()
# Loading in the Data
df = pd.read_csv(filename1, delimiter=",")

df.rename(columns={"<DTYYYYMMDD>": "Date"}, inplace=True)

## Datetime conversion
df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d')

# Dropping any NaNs
df.dropna(inplace=True)

df.rename(columns={"<DTYYYYMMDD>": "Date"}, inplace=True)

df = df.sort_values(by=['Date'])
d = []
for i in range(len(df["Date"])):
    d.append(PersianCal.Christian(str(df["Date"][i])[0:10]).persian_string()) #Tabdil tarikh be Irani
    
    
for i in range(len(df["Date"])):
    df["Date"] = df["Date"].replace(df["Date"][i], d[i])
date_list = []
for date in df["Date"]:
    date_list.append(date[:7])
    
Dates = df["Date"].tolist() #kole tarikh ha
myDates = list(dict.fromkeys(date_list)) #Eshterake Tatikh ha
ext = np.zeros((len(myDates), 2)) #[[max], [min]]
# ind = np.zeros((len(myDates), 1)).tolist()

ind = []

for i in range(len(myDates)):
    ind.append(0)

#first indicator
for date in Dates:
    if (date[:7] in myDates[0]):
        ind1 = Dates.index(date) #یافتن مکان آخرین روز ماه

ind[0] = ind1<jupyter_output><empty_output><jupyter_text>First max and min:<jupyter_code>month_data = [[], [], [], []]
names = ["<Open>", "<High>", "<Low>", "<Close>"]

for i in range(4):
    for j in names:
        month_data[i] = df[j][0:ind1+1].tolist()

ext[0][0] = max(max(a))
ext[0][1] = min(min(a))<jupyter_output><empty_output><jupyter_text>Calculating all of the extremums:<jupyter_code>for i in range(1, len(myDates)):
    for date in Dates:
        if (date[:7] in myDates[i]):
            ind1 = Dates.index(date)
    ind[i] = ind1
    a = [[], [], [], []]
    for ii in range(4):
        for j in names:
            a[ii] = df[j][ind[i-1]+1:ind[i]+1].tolist()
    ext[i][0] = max(max(a))
    ext[i][1] = min(min(a))
# %matplotlib notebook
# import pylab as pp

# pp.style.use("bmh")

# pp.plot(DataF["Date"], DataF["Max"], color='skyblue', label = "Max")
# pp.plot(DataF["Date"], DataF["Min"], color='red', label = "Min")
# pp.legend();<jupyter_output><empty_output><jupyter_text>Index Finder<jupyter_code>def getIndexes(dfObj, value):
    ''' Get index positions of value in dataframe i.e. dfObj.'''
    listOfPos = list()
    # Get bool dataframe with True at positions where the given value exists
    result = dfObj.isin([value])
    # Get list of columns that contains the value
    seriesObj = result.any()
    columnNames = list(seriesObj[seriesObj == True].index)
    # Iterate over list of columns and fetch the rows indexes where value exists
    for col in columnNames:
        rows = list(result[col][result[col] == True].index)
        for row in rows:
            listOfPos.append((row, col))
    # Return a list of tuples indicating the positions of value in the dataframe
    return listOfPos
minDate = []
firstPos = getIndexes(df[0:ind[0]+1], ext[0][1])
minDate.append(df.Date[firstPos[0][0]])

for j in range(len(ext)-1):
    listOfPositions = getIndexes(df[ind[j]+1:ind[j+1]+1], ext[j+1][1])
    for i in range(len(listOfPositions)):
        minDate.append(df.Date[listOfPositions[0][0]])

minDate = list(dict.fromkeys(minDate))
maxDate = []
firstPos = getIndexes(df[0:ind[0]+1], ext[0][0])
maxDate.append(df.Date[firstPos[0][0]])

for j in range(len(ext)-1):
    listOfPositions = getIndexes(df[ind[j]+1:ind[j+1]+1], ext[j+1][0])
    for i in range(len(listOfPositions)):
        maxDate.append(df.Date[listOfPositions[0][0]])
        
maxDate = list(dict.fromkeys(maxDate))
first = []
last = []

for i in range(len(maxDate)):
    if (int(maxDate[i][8:10]) < int(minDate[i][8:10])):
        first.append(ext[i, 0])
        last.append(ext[i, 1])
    else:
        first.append(ext[i, 1])
        last.append(ext[i, 0])
# res = {"Date": myDates,
#        "First": first,
#        "Last": last}
# resF = pd.DataFrame(res)

# # resF.set_index('Date', inplace=True)
# # resF.to_csv(filename + '_output' + '.csv')
#making empty dates
myDates2 = []
for i in range(2*len(myDates)):
    myDates2.append(0)

#Filling Dates with 1 and 15
for i in range(len(myDates)):
    myDates2[2*i] = myDates[i] + '-01'
    myDates2[2*i+1] = myDates[i] + '-15' 
#Making empty final data
final_data = []
for i in range(len(myDates2)):
    final_data.append(0)
    
#Filling final data
for i in range(len(myDates)):
    final_data[2*i] = first[i]
    final_data[2*i+1] = last[i]
result_data = {"Date": myDates2,
               "Extrema": final_data}
result_dataF = pd.DataFrame(result_data)
d2 = []
for i in range(len(result_dataF["Date"])):
    d2.append(PersianCal.Persian
             (str(result_dataF["Date"][i])[0:10]).christian_string2()) #Tabdil tarikh be Irani
    
    
for i in range(len(result_dataF["Date"])):
    result_dataF["Date"] = result_dataF["Date"].replace(result_dataF["Date"][i], d2[i])
result_data2 = {"Date": result_dataF.Date,
                "Open": result_dataF.Extrema,
                "High": result_dataF.Extrema,
                "Low": result_dataF.Extrema,
                "Close": result_dataF.Extrema}

result_data2F = pd.DataFrame(result_data2)
result_data2F.to_csv(filename + '_output' + '.csv')
result_data2F.set_index('Date', inplace=True)
result_data2F.to_csv(filename + '_output' + '.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1842769563649477, 'fraction_numerical': 0.02019473494410386, 'mean_word_length': 4.145640074211503, 'pattern_counts': {'":': 12, '<': 24, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '51615', 'license': 'MIT', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2178, 'n_words': 433, 'path': 'Stocks/Stock max v1.1.ipynb', 'repo_name': 'ali-kin4/JupyterNotes-1', 'size': '5546'}