Convert Bytes into BufferedReader object in Python? - python

The title of this question is the same as this one, and I have voted to reopoen the question.
I want to convert a byte object into a BufferedReader one, and here is my attempts(after referring to many articles):
import numpy as np
from PIL import Image as PILImage
from io import BytesIO
img_np = np.asarray([[[16, 16, 16], [2, 2, 2], [0, 0, 0], [6, 6, 6], [8, 8, 8], [0, 0, 0], [21, 21, 21], [3, 3, 3], [0, 0, 0], [62, 62, 62]], [[0, 0, 0], [71, 71, 71], [142, 142, 142], [107, 107, 107], [99, 99, 99], [101, 101, 101], [4, 4, 4], [86, 86, 86], [99, 99, 99], [146, 146, 146]], [[162, 162, 162], [203, 203, 203], [192, 192, 192], [228, 228, 228], [191, 191, 191], [178, 178, 178], [222, 222, 222], [200, 200, 200], [198, 198, 198], [182, 182, 182]], [[117, 117, 117], [178, 178, 178], [199, 199, 199], [214, 214, 214], [222, 222, 222], [208, 208, 208], [255, 255, 255], [251, 251, 251], [219, 219, 219], [255, 255, 255]], [[0, 0, 0], [0, 0, 0], [80, 80, 80], [169, 169, 169], [193, 193, 193], [238, 238, 238], [239, 239, 239], [243, 243, 243], [254, 254, 254], [230, 230, 230]], [[20, 20, 20], [20, 20, 20], [9, 9, 9], [1, 1, 1], [130, 130, 130], [194, 194, 194], [216, 216, 216], [255, 255, 255], [252, 252, 252], [255, 255, 255]], [[9, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [3, 3, 3], [44, 44, 44], [191, 191, 191], [217, 217, 217], [248, 248, 248], [225, 225, 225]], [[0, 0, 0], [11, 11, 11], [3, 3, 3], [11, 11, 11], [6, 6, 6], [15, 15, 15], [0, 0, 0], [153, 153, 153], [255, 255, 255], [253, 253, 253]], [[0, 0, 0], [5, 5, 5], [1, 1, 1], [4, 4, 4], [8, 8, 8], [4, 4, 4], [3, 3, 3], [0, 0, 0], [159, 159, 159], [241, 241, 241]], [[10, 10, 10], [9, 9, 9], [6, 6, 6], [2, 2, 2], [0, 0, 0], [0, 0, 0], [3, 3, 3], [20, 20, 20], [0, 0, 0], [185, 185, 185]]])
im = PILImage.fromarray(img_np.astype(np.uint8))
# im.save('./temp.jpeg', "JPEG")
# f = open('./temp.jpeg', 'rb')
# print(type(f))
#
b_handle = io.BytesIO()
im.save(b_handle, format="JPEG")
# b = im.tobytes()
print(type(b_handle))
b = b_handle.read()
print(type(b))
print(b)
im.save(b_handle, format="JPEG")
b_br = io.BufferedReader(b_handle)
print(type(b_br))
b = b_br.read()
print(type(b))
print(b)
The output is as below:
<class '_io.BytesIO'>
<class 'bytes'>
b''
<class '_io.BufferedReader'>
<class 'bytes'>
b''
It seems that the file like objects are empty. I know that for the b_handle I can get the value by b_handle.getvalue() but for the bufferedreader it doesn't work as a file object.
How can I convert a byte string into a bufferedreader object, the same as I open a file?

You are almost there. Once you save the image bytes into the buffer you need to seek(Change stream position) to byte offset 0 prior to the read call.
b_handle = io.BytesIO()
im.save(b_handle, format="JPEG")
b_handle.seek(0)
b_handle.name = "temp.jpeg"
b_br = io.BufferedReader(b_handle)
b = b_br.read()
Example,
>>> from io import BytesIO, BufferedReader
>>>
>>> b_handle = BytesIO()
>>> b_handle.write(b"Hello World")
11
>>> b_handle.seek(0) # This is important.
0
>>> br = BufferedReader(b_handle)
>>> br
<_io.BufferedReader>
>>> br.read()
b'Hello World'

Related

How to interchange an element within multidimensional list in python?

I'm unable to change the elements of a multidimensional list. The code does work on a simple list, but not on a lists within a list. I assume there is a correct way of modifying a multidimensional list?
The code I'm stuck with:
curpos = [[88, 118, 1], [200, 118, 0], [312, 118, 2],
[88, 230, 3], [200, 230, 4], [312, 230, 5],
[88, 342, 6], [200, 342, 7], [312, 342, 8]]
movls = ['right', 'down', 'left']
def move_tile(current_loc, movlist):
result = []
temp = current_loc.copy()
for i in temp:
if i[2] == 0:
empty = temp.index(i)
for j in movlist:
if j == 'right':
temp[empty][2] = temp[empty+1][2] # the problem is here, it changes the origin current_loc instead of the copy temp
temp[empty+1][2] = 0
elif j == 'down':
temp[empty][2] = temp[empty+3][2]
temp[empty+3][2] = 0
elif j == 'left':
temp[empty][2] = temp[empty-1][2]
temp[empty-1][2] = 0
elif j == 'up':
temp[empty][2] = temp[empty-3][2]
temp[empty-3][2] = 0
print(temp) # this outputs [[88, 118, 1], [200, 118, 2], [312, 118, 0], [88, 230, 3], [200, 230, 4], [312, 230, 5], [88, 342, 6], [200, 342, 7], [312, 342, 8]]
result.append(temp) # but this appends [88, 118, 0], [200, 118, 1], [312, 118, 0], [88, 230, 3], [200, 230, 0], [312, 230, 5], [88, 342, 6], [200, 342, 7], [312, 342, 8]
temp = current_loc.copy() # does not reset the temporary var
return result
print(move_tile(curpos, movls))
The correct output (without the print(temp) should be something like this:
[[[88, 118, 1], [200, 118, 2], [312, 118, 0],
[88, 230, 3], [200, 230, 4], [312, 230, 5],
[88, 342, 6], [200, 342, 7], [312, 342, 8]],
[[88, 118, 1], [200, 118, 4], [312, 118, 2],
[88, 230, 3], [200, 230, 0], [312, 230, 5],
[88, 342, 6], [200, 342, 7], [312, 342, 8]],
[[88, 118, 0], [200, 118, 1], [312, 118, 2],
[88, 230, 3], [200, 230, 4], [312, 230, 5],
[88, 342, 6], [200, 342, 7], [312, 342, 8]]]
You can use deepcopy to ensure that temp is a separate list. Making this change will generate the output that you have described above.
from copy import deepcopy
curpos = [[88, 118, 1], [200, 118, 0], [312, 118, 2],
[88, 230, 3], [200, 230, 4], [312, 230, 5],
[88, 342, 6], [200, 342, 7], [312, 342, 8]]
movls = ['right', 'down', 'left']
def move_tile(current_loc, movlist):
result = []
temp = deepcopy(current_loc)
for i in temp:
if i[2] == 0:
empty = temp.index(i)
for j in movlist:
if j == 'right':
temp[empty][2] = temp[empty+1][2]
temp[empty+1][2] = 0
elif j == 'down':
temp[empty][2] = temp[empty+3][2]
temp[empty+3][2] = 0
elif j == 'left':
temp[empty][2] = temp[empty-1][2]
temp[empty-1][2] = 0
elif j == 'up':
temp[empty][2] = temp[empty-3][2]
temp[empty-3][2] = 0
result.append(temp)
temp = deepcopy(current_loc)
return result
print(move_tile(curpos, movls))

(34, 'Numerical result out of range') calculating Annual Percentage Rate with scipy

I am trying to solve Annual Percentage Rate in Python using scipy library and newton method. At some point Python reaches max Pythonic float range solving this equation:
Here is example code with data that fails. Is there anything that I can use (interval etc.) that is able to solve this equation?
from scipy.optimize import newton
class ARP:
def __init__(self):
self.year_days = 365
self.table = [[0, 416.0, '2022-04-01', 0, 0, 0, 0, 0], [1, 75.92, '2022-04-02', 1, 1, 0, 0, 0], [2, 75.92, '2022-05-02', 30, 1, 0, 1, 0], [3, 90.92, '2022-06-02', 31, 1, 0, 2, 0], [4, 75.92, '2022-07-02', 30, 1, 0, 3, 0], [5, 75.92, '2022-08-02', 31, 1, 0, 4, 0], [6, 90.92, '2022-09-02', 31, 1, 0, 5, 0], [7, 75.92, '2022-10-02', 30, 1, 0, 6, 0], [8, 90.92, '2022-11-02', 31, 1, 0, 7, 0], [9, 75.92, '2022-12-02', 30, 1, 0, 8, 0], [10, 75.92, '2023-01-02', 31, 1, 0, 9, 0], [11, 90.92, '2023-02-02', 31, 1, 0, 10, 0], [12, 75.92, '2023-03-02', 28, 1, 0, 11, 0], [13, 90.92, '2023-04-02', 31, 1, 0, 12, 0], [14, 782.9, '2023-05-02', 30, 1, 0, 13, 0], [15, 732.9, '2023-06-02', 31, 1, 0, 14, 0], [16, 732.9, '2023-07-02', 30, 1, 0, 15, 0], [17, 732.9, '2023-08-02', 31, 1, 0, 16, 0], [18, 732.9, '2023-09-02', 31, 1, 0, 17, 0], [19, 732.9, '2023-10-02', 30, 1, 0, 18, 0], [20, 732.9, '2023-11-02', 31, 1, 0, 19, 0], [21, 732.9, '2023-12-02', 30, 1, 0, 20, 0], [22, 732.9, '2024-01-02', 31, 1, 0, 21, 0], [23, 732.9, '2024-02-02', 31, 1, 0, 22, 0], [24, 732.9, '2024-03-02', 29, 1, 0, 23, 0], [25, 732.9, '2024-04-02', 31, 1, 0, 24, 0], [26, 782.9, '2024-05-02', 30, 1, 0, 25, 0], [27, 732.9, '2024-06-02', 31, 1, 0, 26, 0], [28, 732.9, '2024-07-02', 30, 1, 0, 27, 0], [29, 732.9, '2024-08-02', 31, 1, 0, 28, 0], [30, 732.9, '2024-09-02', 31, 1, 0, 29, 0], [31, 732.9, '2024-10-02', 30, 1, 0, 30, 0], [32, 732.9, '2024-11-02', 31, 1, 0, 31, 0], [33, 732.9, '2024-12-02', 30, 1, 0, 32, 0], [34, 732.9, '2025-01-02', 31, 1, 0, 33, 0], [35, 732.9, '2025-02-02', 31, 1, 0, 34, 0], [36, 732.9, '2025-03-02', 28, 1, 0, 35, 0], [37, 732.9, '2025-04-02', 31, 1, 0, 36, 0], [38, 782.9, '2025-05-02', 30, 1, 0, 37, 0], [39, 732.9, '2025-06-02', 31, 1, 0, 38, 0], [40, 732.9, '2025-07-02', 30, 1, 0, 39, 0], [41, 732.9, '2025-08-02', 31, 1, 0, 40, 0], [42, 732.9, '2025-09-02', 31, 1, 0, 41, 0], [43, 732.9, '2025-10-02', 30, 1, 0, 42, 0], [44, 732.9, '2025-11-02', 31, 1, 0, 43, 0], [45, 732.9, '2025-12-02', 30, 1, 0, 44, 0], [46, 732.9, '2026-01-02', 31, 1, 0, 45, 0], [47, 732.9, '2026-02-02', 31, 1, 0, 46, 0], [48, 732.9, '2026-03-02', 28, 1, 0, 47, 0], [49, 732.9, '2026-04-02', 31, 1, 0, 48, 0], [50, 782.9, '2026-05-02', 30, 1, 0, 49, 0], [51, 732.9, '2026-06-02', 31, 1, 0, 50, 0], [52, 732.9, '2026-07-02', 30, 1, 0, 51, 0], [53, 732.9, '2026-08-02', 31, 1, 0, 52, 0], [54, 732.9, '2026-09-02', 31, 1, 0, 53, 0], [55, 732.9, '2026-10-02', 30, 1, 0, 54, 0], [56, 732.9, '2026-11-02', 31, 1, 0, 55, 0], [57, 732.9, '2026-12-02', 30, 1, 0, 56, 0], [58, 732.9, '2027-01-02', 31, 1, 0, 57, 0], [59, 732.9, '2027-02-02', 31, 1, 0, 58, 0], [60, 732.9, '2027-03-02', 28, 1, 0, 59, 0], [61, 732.9, '2027-04-02', 31, 1, 0, 60, 0], [62, 782.9, '2027-05-02', 30, 1, 0, 61, 0], [63, 732.9, '2027-06-02', 31, 1, 0, 62, 0], [64, 732.9, '2027-07-02', 30, 1, 0, 63, 0], [65, 732.9, '2027-08-02', 31, 1, 0, 64, 0], [66, 732.9, '2027-09-02', 31, 1, 0, 65, 0], [67, 732.9, '2027-10-02', 30, 1, 0, 66, 0], [68, 732.9, '2027-11-02', 31, 1, 0, 67, 0], [69, 732.9, '2027-12-02', 30, 1, 0, 68, 0], [70, 732.9, '2028-01-02', 31, 1, 0, 69, 0], [71, 732.9, '2028-02-02', 31, 1, 0, 70, 0], [72, 732.9, '2028-03-02', 29, 1, 0, 71, 0], [73, 732.9, '2028-04-02', 31, 1, 0, 72, 0], [74, 782.9, '2028-05-02', 30, 1, 0, 73, 0], [75, 732.9, '2028-06-02', 31, 1, 0, 74, 0], [76, 732.9, '2028-07-02', 30, 1, 0, 75, 0], [77, 732.9, '2028-08-02', 31, 1, 0, 76, 0], [78, 732.9, '2028-09-02', 31, 1, 0, 77, 0], [79, 732.9, '2028-10-02', 30, 1, 0, 78, 0], [80, 732.9, '2028-11-02', 31, 1, 0, 79, 0], [81, 732.9, '2028-12-02', 30, 1, 0, 80, 0], [82, 732.9, '2029-01-02', 31, 1, 0, 81, 0], [83, 732.9, '2029-02-02', 31, 1, 0, 82, 0], [84, 732.9, '2029-03-02', 28, 1, 0, 83, 0], [85, 732.9, '2029-04-02', 31, 1, 0, 84, 0], [86, 782.9, '2029-05-02', 30, 1, 0, 85, 0], [87, 732.9, '2029-06-02', 31, 1, 0, 86, 0], [88, 732.9, '2029-07-02', 30, 1, 0, 87, 0], [89, 732.9, '2029-08-02', 31, 1, 0, 88, 0], [90, 732.9, '2029-09-02', 31, 1, 0, 89, 0], [91, 732.9, '2029-10-02', 30, 1, 0, 90, 0], [92, 732.9, '2029-11-02', 31, 1, 0, 91, 0], [93, 732.9, '2029-12-02', 30, 1, 0, 92, 0], [94, 732.9, '2030-01-02', 31, 1, 0, 93, 0], [95, 732.9, '2030-02-02', 31, 1, 0, 94, 0], [96, 732.9, '2030-03-02', 28, 1, 0, 95, 0], [97, 732.9, '2030-04-02', 31, 1, 0, 96, 0], [98, 782.9, '2030-05-02', 30, 1, 0, 97, 0], [99, 732.9, '2030-06-02', 31, 1, 0, 98, 0], [100, 732.9, '2030-07-02', 30, 1, 0, 99, 0], [101, 732.9, '2030-08-02', 31, 1, 0, 100, 0], [102, 732.9, '2030-09-02', 31, 1, 0, 101, 0], [103, 732.9, '2030-10-02', 30, 1, 0, 102, 0], [104, 732.9, '2030-11-02', 31, 1, 0, 103, 0], [105, 732.9, '2030-12-02', 30, 1, 0, 104, 0], [106, 732.9, '2031-01-02', 31, 1, 0, 105, 0], [107, 732.9, '2031-02-02', 31, 1, 0, 106, 0], [108, 732.9, '2031-03-02', 28, 1, 0, 107, 0], [109, 732.9, '2031-04-02', 31, 1, 0, 108, 0], [110, 782.9, '2031-05-02', 30, 1, 0, 109, 0], [111, 732.9, '2031-06-02', 31, 1, 0, 110, 0], [112, 732.9, '2031-07-02', 30, 1, 0, 111, 0], [113, 732.9, '2031-08-02', 31, 1, 0, 112, 0], [114, 732.9, '2031-09-02', 31, 1, 0, 113, 0], [115, 732.9, '2031-10-02', 30, 1, 0, 114, 0], [116, 732.9, '2031-11-02', 31, 1, 0, 115, 0], [117, 732.9, '2031-12-02', 30, 1, 0, 116, 0], [118, 732.9, '2032-01-02', 31, 1, 0, 117, 0], [119, 732.9, '2032-02-02', 31, 1, 0, 118, 0], [120, 732.9, '2032-03-02', 29, 1, 0, 119, 0], [121, 732.9, '2032-04-02', 31, 1, 0, 120, 0], [122, 782.9, '2032-05-02', 30, 1, 0, 121, 0], [123, 732.9, '2032-06-02', 31, 1, 0, 122, 0], [124, 732.9, '2032-07-02', 30, 1, 0, 123, 0], [125, 732.9, '2032-08-02', 31, 1, 0, 124, 0], [126, 732.9, '2032-09-02', 31, 1, 0, 125, 0], [127, 732.9, '2032-10-02', 30, 1, 0, 126, 0], [128, 732.9, '2032-11-02', 31, 1, 0, 127, 0], [129, 732.9, '2032-12-02', 30, 1, 0, 128, 0], [130, 732.9, '2033-01-02', 31, 1, 0, 129, 0], [131, 732.9, '2033-02-02', 31, 1, 0, 130, 0], [132, 732.9, '2033-03-02', 28, 1, 0, 131, 0], [133, 732.9, '2033-04-02', 31, 1, 0, 132, 0], [134, 782.9, '2033-05-02', 30, 1, 0, 133, 0], [135, 732.9, '2033-06-02', 31, 1, 0, 134, 0], [136, 732.9, '2033-07-02', 30, 1, 0, 135, 0], [137, 732.9, '2033-08-02', 31, 1, 0, 136, 0], [138, 732.9, '2033-09-02', 31, 1, 0, 137, 0], [139, 732.9, '2033-10-02', 30, 1, 0, 138, 0], [140, 732.9, '2033-11-02', 31, 1, 0, 139, 0], [141, 732.9, '2033-12-02', 30, 1, 0, 140, 0], [142, 732.9, '2034-01-02', 31, 1, 0, 141, 0], [143, 732.9, '2034-02-02', 31, 1, 0, 142, 0], [144, 732.9, '2034-03-02', 28, 1, 0, 143, 0], [145, 732.9, '2034-04-02', 31, 1, 0, 144, 0], [146, 782.9, '2034-05-02', 30, 1, 0, 145, 0], [147, 732.9, '2034-06-02', 31, 1, 0, 146, 0], [148, 732.9, '2034-07-02', 30, 1, 0, 147, 0], [149, 732.9, '2034-08-02', 31, 1, 0, 148, 0], [150, 732.9, '2034-09-02', 31, 1, 0, 149, 0], [151, 732.9, '2034-10-02', 30, 1, 0, 150, 0], [152, 732.9, '2034-11-02', 31, 1, 0, 151, 0], [153, 732.9, '2034-12-02', 30, 1, 0, 152, 0], [154, 732.9, '2035-01-02', 31, 1, 0, 153, 0], [155, 732.9, '2035-02-02', 31, 1, 0, 154, 0], [156, 732.9, '2035-03-02', 28, 1, 0, 155, 0], [157, 732.9, '2035-04-02', 31, 1, 0, 156, 0], [158, 782.9, '2035-05-02', 30, 1, 0, 157, 0], [159, 732.9, '2035-06-02', 31, 1, 0, 158, 0], [160, 732.9, '2035-07-02', 30, 1, 0, 159, 0], [161, 732.9, '2035-08-02', 31, 1, 0, 160, 0], [162, 732.9, '2035-09-02', 31, 1, 0, 161, 0], [163, 732.9, '2035-10-02', 30, 1, 0, 162, 0], [164, 732.9, '2035-11-02', 31, 1, 0, 163, 0], [165, 732.9, '2035-12-02', 30, 1, 0, 164, 0], [166, 732.9, '2036-01-02', 31, 1, 0, 165, 0], [167, 732.9, '2036-02-02', 31, 1, 0, 166, 0], [168, 732.9, '2036-03-02', 29, 1, 0, 167, 0], [169, 732.9, '2036-04-02', 31, 1, 0, 168, 0], [170, 782.9, '2036-05-02', 30, 1, 0, 169, 0], [171, 732.9, '2036-06-02', 31, 1, 0, 170, 0], [172, 732.9, '2036-07-02', 30, 1, 0, 171, 0], [173, 732.9, '2036-08-02', 31, 1, 0, 172, 0], [174, 732.9, '2036-09-02', 31, 1, 0, 173, 0], [175, 732.9, '2036-10-02', 30, 1, 0, 174, 0], [176, 732.9, '2036-11-02', 31, 1, 0, 175, 0], [177, 732.9, '2036-12-02', 30, 1, 0, 176, 0], [178, 732.9, '2037-01-02', 31, 1, 0, 177, 0], [179, 732.9, '2037-02-02', 31, 1, 0, 178, 0], [180, 732.9, '2037-03-02', 28, 1, 0, 179, 0], [181, 732.9, '2037-04-02', 31, 1, 0, 180, 0], [182, 782.9, '2037-05-02', 30, 1, 0, 181, 0], [183, 732.9, '2037-06-02', 31, 1, 0, 182, 0], [184, 732.9, '2037-07-02', 30, 1, 0, 183, 0], [185, 732.9, '2037-08-02', 31, 1, 0, 184, 0], [186, 732.9, '2037-09-02', 31, 1, 0, 185, 0], [187, 732.9, '2037-10-02', 30, 1, 0, 186, 0], [188, 732.9, '2037-11-02', 31, 1, 0, 187, 0], [189, 732.9, '2037-12-02', 30, 1, 0, 188, 0], [190, 732.9, '2038-01-02', 31, 1, 0, 189, 0], [191, 732.9, '2038-02-02', 31, 1, 0, 190, 0], [192, 732.9, '2038-03-02', 28, 1, 0, 191, 0], [193, 732.9, '2038-04-02', 31, 1, 0, 192, 0], [194, 782.9, '2038-05-02', 30, 1, 0, 193, 0], [195, 732.9, '2038-06-02', 31, 1, 0, 194, 0], [196, 732.9, '2038-07-02', 30, 1, 0, 195, 0], [197, 732.9, '2038-08-02', 31, 1, 0, 196, 0], [198, 732.9, '2038-09-02', 31, 1, 0, 197, 0], [199, 732.9, '2038-10-02', 30, 1, 0, 198, 0], [200, 732.9, '2038-11-02', 31, 1, 0, 199, 0], [201, 732.9, '2038-12-02', 30, 1, 0, 200, 0], [202, 732.9, '2039-01-02', 31, 1, 0, 201, 0], [203, 732.9, '2039-02-02', 31, 1, 0, 202, 0], [204, 732.9, '2039-03-02', 28, 1, 0, 203, 0], [205, 732.9, '2039-04-02', 31, 1, 0, 204, 0], [206, 782.9, '2039-05-02', 30, 1, 0, 205, 0], [207, 732.9, '2039-06-02', 31, 1, 0, 206, 0], [208, 732.9, '2039-07-02', 30, 1, 0, 207, 0], [209, 732.9, '2039-08-02', 31, 1, 0, 208, 0], [210, 732.9, '2039-09-02', 31, 1, 0, 209, 0], [211, 732.9, '2039-10-02', 30, 1, 0, 210, 0], [212, 732.9, '2039-11-02', 31, 1, 0, 211, 0], [213, 732.9, '2039-12-02', 30, 1, 0, 212, 0], [214, 732.9, '2040-01-02', 31, 1, 0, 213, 0], [215, 732.9, '2040-02-02', 31, 1, 0, 214, 0], [216, 732.9, '2040-03-02', 29, 1, 0, 215, 0], [217, 732.9, '2040-04-02', 31, 1, 0, 216, 0], [218, 782.9, '2040-05-02', 30, 1, 0, 217, 0], [219, 732.9, '2040-06-02', 31, 1, 0, 218, 0], [220, 732.9, '2040-07-02', 30, 1, 0, 219, 0], [221, 732.9, '2040-08-02', 31, 1, 0, 220, 0], [222, 732.9, '2040-09-02', 31, 1, 0, 221, 0], [223, 732.9, '2040-10-02', 30, 1, 0, 222, 0], [224, 732.9, '2040-11-02', 31, 1, 0, 223, 0], [225, 732.9, '2040-12-02', 30, 1, 0, 224, 0], [226, 732.9, '2041-01-02', 31, 1, 0, 225, 0], [227, 732.9, '2041-02-02', 31, 1, 0, 226, 0], [228, 732.9, '2041-03-02', 28, 1, 0, 227, 0], [229, 732.9, '2041-04-02', 31, 1, 0, 228, 0], [230, 782.9, '2041-05-02', 30, 1, 0, 229, 0], [231, 732.9, '2041-06-02', 31, 1, 0, 230, 0], [232, 732.9, '2041-07-02', 30, 1, 0, 231, 0], [233, 732.9, '2041-08-02', 31, 1, 0, 232, 0], [234, 732.9, '2041-09-02', 31, 1, 0, 233, 0], [235, 732.9, '2041-10-02', 30, 1, 0, 234, 0], [236, 732.9, '2041-11-02', 31, 1, 0, 235, 0], [237, 732.9, '2041-12-02', 30, 1, 0, 236, 0], [238, 732.9, '2042-01-02', 31, 1, 0, 237, 0], [239, 732.9, '2042-02-02', 31, 1, 0, 238, 0], [240, 732.9, '2042-03-02', 28, 1, 0, 239, 0], [241, 732.9, '2042-04-02', 31, 1, 0, 240, 0], [242, 782.9, '2042-05-02', 30, 1, 0, 241, 0], [243, 732.9, '2042-06-02', 31, 1, 0, 242, 0], [244, 732.9, '2042-07-02', 30, 1, 0, 243, 0], [245, 732.9, '2042-08-02', 31, 1, 0, 244, 0], [246, 732.9, '2042-09-02', 31, 1, 0, 245, 0], [247, 732.9, '2042-10-02', 30, 1, 0, 246, 0], [248, 732.9, '2042-11-02', 31, 1, 0, 247, 0], [249, 732.9, '2042-12-02', 30, 1, 0, 248, 0], [250, 732.9, '2043-01-02', 31, 1, 0, 249, 0], [251, 732.9, '2043-02-02', 31, 1, 0, 250, 0], [252, 732.9, '2043-03-02', 28, 1, 0, 251, 0], [253, 732.9, '2043-04-02', 31, 1, 0, 252, 0], [254, 782.9, '2043-05-02', 30, 1, 0, 253, 0], [255, 732.9, '2043-06-02', 31, 1, 0, 254, 0], [256, 732.9, '2043-07-02', 30, 1, 0, 255, 0], [257, 732.9, '2043-08-02', 31, 1, 0, 256, 0], [258, 732.9, '2043-09-02', 31, 1, 0, 257, 0], [259, 732.9, '2043-10-02', 30, 1, 0, 258, 0], [260, 732.9, '2043-11-02', 31, 1, 0, 259, 0], [261, 732.9, '2043-12-02', 30, 1, 0, 260, 0], [262, 732.9, '2044-01-02', 31, 1, 0, 261, 0], [263, 732.9, '2044-02-02', 31, 1, 0, 262, 0], [264, 732.9, '2044-03-02', 29, 1, 0, 263, 0], [265, 732.9, '2044-04-02', 31, 1, 0, 264, 0], [266, 782.9, '2044-05-02', 30, 1, 0, 265, 0], [267, 732.9, '2044-06-02', 31, 1, 0, 266, 0], [268, 732.9, '2044-07-02', 30, 1, 0, 267, 0], [269, 732.9, '2044-08-02', 31, 1, 0, 268, 0], [270, 732.9, '2044-09-02', 31, 1, 0, 269, 0], [271, 732.9, '2044-10-02', 30, 1, 0, 270, 0], [272, 732.9, '2044-11-02', 31, 1, 0, 271, 0], [273, 732.9, '2044-12-02', 30, 1, 0, 272, 0], [274, 732.9, '2045-01-02', 31, 1, 0, 273, 0], [275, 732.9, '2045-02-02', 31, 1, 0, 274, 0], [276, 732.9, '2045-03-02', 28, 1, 0, 275, 0], [277, 732.9, '2045-04-02', 31, 1, 0, 276, 0], [278, 782.9, '2045-05-02', 30, 1, 0, 277, 0], [279, 732.9, '2045-06-02', 31, 1, 0, 278, 0], [280, 732.9, '2045-07-02', 30, 1, 0, 279, 0], [281, 732.9, '2045-08-02', 31, 1, 0, 280, 0], [282, 732.9, '2045-09-02', 31, 1, 0, 281, 0], [283, 732.9, '2045-10-02', 30, 1, 0, 282, 0], [284, 732.9, '2045-11-02', 31, 1, 0, 283, 0], [285, 732.9, '2045-12-02', 30, 1, 0, 284, 0], [286, 732.9, '2046-01-02', 31, 1, 0, 285, 0], [287, 732.9, '2046-02-02', 31, 1, 0, 286, 0], [288, 732.9, '2046-03-02', 28, 1, 0, 287, 0], [289, 732.9, '2046-04-02', 31, 1, 0, 288, 0], [290, 782.9, '2046-05-02', 30, 1, 0, 289, 0], [291, 732.9, '2046-06-02', 31, 1, 0, 290, 0], [292, 732.9, '2046-07-02', 30, 1, 0, 291, 0], [293, 732.9, '2046-08-02', 31, 1, 0, 292, 0], [294, 732.9, '2046-09-02', 31, 1, 0, 293, 0], [295, 732.9, '2046-10-02', 30, 1, 0, 294, 0], [296, 732.9, '2046-11-02', 31, 1, 0, 295, 0], [297, 732.9, '2046-12-02', 30, 1, 0, 296, 0], [298, 732.9, '2047-01-02', 31, 1, 0, 297, 0], [299, 732.9, '2047-02-02', 31, 1, 0, 298, 0], [300, 732.9, '2047-03-02', 28, 1, 0, 299, 0], [301, 732.9, '2047-04-02', 31, 1, 0, 300, 0], [302, 782.9, '2047-05-02', 30, 1, 0, 301, 0], [303, 732.9, '2047-06-02', 31, 1, 0, 302, 0], [304, 732.9, '2047-07-02', 30, 1, 0, 303, 0], [305, 732.9, '2047-08-02', 31, 1, 0, 304, 0], [306, 732.9, '2047-09-02', 31, 1, 0, 305, 0], [307, 732.9, '2047-10-02', 30, 1, 0, 306, 0], [308, 732.9, '2047-11-02', 31, 1, 0, 307, 0], [309, 732.9, '2047-12-02', 30, 1, 0, 308, 0], [310, 732.9, '2048-01-02', 31, 1, 0, 309, 0], [311, 732.9, '2048-02-02', 31, 1, 0, 310, 0], [312, 732.9, '2048-03-02', 29, 1, 0, 311, 0], [313, 732.9, '2048-04-02', 31, 1, 0, 312, 0], [314, 782.9, '2048-05-02', 30, 1, 0, 313, 0], [315, 732.9, '2048-06-02', 31, 1, 0, 314, 0], [316, 732.9, '2048-07-02', 30, 1, 0, 315, 0], [317, 732.9, '2048-08-02', 31, 1, 0, 316, 0], [318, 732.9, '2048-09-02', 31, 1, 0, 317, 0], [319, 732.9, '2048-10-02', 30, 1, 0, 318, 0], [320, 732.9, '2048-11-02', 31, 1, 0, 319, 0], [321, 732.9, '2048-12-02', 30, 1, 0, 320, 0], [322, 732.9, '2049-01-02', 31, 1, 0, 321, 0], [323, 732.9, '2049-02-02', 31, 1, 0, 322, 0], [324, 732.9, '2049-03-02', 28, 1, 0, 323, 0], [325, 732.9, '2049-04-02', 31, 1, 0, 324, 0], [326, 782.9, '2049-05-02', 30, 1, 0, 325, 0], [327, 732.9, '2049-06-02', 31, 1, 0, 326, 0], [328, 732.9, '2049-07-02', 30, 1, 0, 327, 0], [329, 732.9, '2049-08-02', 31, 1, 0, 328, 0], [330, 732.9, '2049-09-02', 31, 1, 0, 329, 0], [331, 732.9, '2049-10-02', 30, 1, 0, 330, 0], [332, 732.9, '2049-11-02', 31, 1, 0, 331, 0], [333, 732.9, '2049-12-02', 30, 1, 0, 332, 0], [334, 732.9, '2050-01-02', 31, 1, 0, 333, 0], [335, 732.9, '2050-02-02', 31, 1, 0, 334, 0], [336, 732.9, '2050-03-02', 28, 1, 0, 335, 0], [337, 732.9, '2050-04-02', 31, 1, 0, 336, 0], [338, 782.9, '2050-05-02', 30, 1, 0, 337, 0], [339, 732.9, '2050-06-02', 31, 1, 0, 338, 0], [340, 732.9, '2050-07-02', 30, 1, 0, 339, 0], [341, 732.9, '2050-08-02', 31, 1, 0, 340, 0], [342, 732.9, '2050-09-02', 31, 1, 0, 341, 0], [343, 732.9, '2050-10-02', 30, 1, 0, 342, 0], [344, 732.9, '2050-11-02', 31, 1, 0, 343, 0], [345, 732.9, '2050-12-02', 30, 1, 0, 344, 0], [346, 732.9, '2051-01-02', 31, 1, 0, 345, 0], [347, 732.9, '2051-02-02', 31, 1, 0, 346, 0], [348, 732.9, '2051-03-02', 28, 1, 0, 347, 0], [349, 732.9, '2051-04-02', 31, 1, 0, 348, 0], [350, 782.9, '2051-05-02', 30, 1, 0, 349, 0], [351, 732.9, '2051-06-02', 31, 1, 0, 350, 0], [352, 732.9, '2051-07-02', 30, 1, 0, 351, 0], [353, 732.9, '2051-08-02', 31, 1, 0, 352, 0], [354, 732.9, '2051-09-02', 31, 1, 0, 353, 0], [355, 732.9, '2051-10-02', 30, 1, 0, 354, 0], [356, 732.9, '2051-11-02', 31, 1, 0, 355, 0], [357, 732.9, '2051-12-02', 30, 1, 0, 356, 0], [358, 732.9, '2052-01-02', 31, 1, 0, 357, 0], [359, 732.9, '2052-02-02', 31, 1, 0, 358, 0], [360, 732.9, '2052-03-02', 29, 1, 0, 359, 0], [361, 732.9, '2052-04-02', 31, 1, 0, 360, 0], [362, 782.9, '2052-05-02', 30, 1, 0, 361, 0], [363, 732.9, '2052-06-02', 31, 1, 0, 362, 0], [364, 732.9, '2052-07-02', 30, 1, 0, 363, 0], [365, 732.9, '2052-08-02', 31, 1, 0, 364, 0], [366, 732.9, '2052-09-02', 31, 1, 0, 365, 0], [367, 732.9, '2052-10-02', 30, 1, 0, 366, 0], [368, 732.9, '2052-11-02', 31, 1, 0, 367, 0], [369, 732.9, '2052-12-02', 30, 1, 0, 368, 0], [370, 732.9, '2053-01-02', 31, 1, 0, 369, 0], [371, 732.9, '2053-02-02', 31, 1, 0, 370, 0], [372, 732.9, '2053-03-02', 28, 1, 0, 371, 0], [373, 732.9, '2053-04-02', 31, 1, 0, 372, 0], [374, 726.66, '2053-05-02', 30, 1, 0, 373, 0]]
self.drawing_calendar = [[0, '50000', '2022-04-01', 0, 0, 0, 0, 0], [1, '45000', '2022-06-13', 73, 30, 0, 5, 0], [2, '45000', '2022-08-25', 73, 72, 0, 6, 0], [3, '40000', '2022-11-06', 73, 72, 0, 7, 0], [4, '40000', '2023-01-18', 73, 72, 0, 8, 0], [5, '20000', '2023-04-01', 73, 72, 0, 9, 0]]
def calculate_arp(self):
try:
result = newton(self.arp_general, 0)
print("ARP result: ", result)
except Exception as e:
print(f"Calculation failed! Exception: {e}")
def arp_general(self, x):
left_sum = 0
right_sum = 0
for i in range(len(self.drawing_calendar)):
amount = float(self.table[i][1])
day_part = float(self.table[i][4] / self.year_days)
week_part = float(self.table[i][5] / 52)
month_part = float((self.table[i][6]) / 12)
year_part = self.table[i][7]
coef_pow = year_part + month_part + week_part + day_part
left_sum += amount / ((1 + x) ** coef_pow)
for i in range(len(self.table)):
amount = float(self.table[i][1])
day_part = float(self.table[i][4] / self.year_days)
week_part = float(self.table[i][5] / 52)
month_part = float((self.table[i][6]) / 12)
year_part = self.table[i][7]
coef_pow = year_part + month_part + week_part + day_part
right_sum += amount / ((1 + x) ** coef_pow)
return left_sum - right_sum
a = ARP()
a.calculate_arp()
I found a bug with help of my friend. The problem was that left_sum used wrong data. It should use data from self.drawing_calendar instead of self.table.

why indexing ndarray of picture like this makes picture green and blue?

import numpy as np
from skimage import io
import matplotlib.pyplot as plt
jeju = io.imread('jeju.jpg')
jeju.shape
> (960,1280,3)
jeju
> Array([[[171, 222, 251],
[172, 223, 252],
[172, 223, 252],
...,
[124, 189, 255],
[121, 189, 254],
[120, 188, 253]],
[[173, 224, 253],
[173, 224, 253],
[173, 224, 253],
...,
[124, 189, 255],
[122, 190, 255],
[121, 189, 254]],
[[174, 225, 254],
[174, 225, 254],
[175, 226, 255]
...,
[125, 190, 255],
[122, 190, 255],
[122, 190, 255]],
...,
[[ 66, 93, 26],
[ 89, 114, 46],
[ 49, 72, 2],
...,
[ 2, 29, 0],
[ 34, 59, 17],
[ 40, 63, 21]],
[[ 44, 71, 4],
[ 23, 50, 0],
[ 29, 52, 0],
...,
[ 40, 67, 22],
[ 0, 19, 0],
[ 16, 41, 0]],
[[ 29, 58, 0],
[ 44, 71, 2],
[ 84, 110, 37],
...,
[ 17, 44, 1],
[ 33, 60, 17],
[ 18, 43, 1]]], dtype=uint8)
plt.imshow(jeju)
plt.imshow(jeju[:,:,0])
jeju[:,:,0]
>Array([[171, 172, 172, ..., 124, 121, 120],
[173, 173, 173, ..., 124, 122, 121],
[174, 174, 175, ..., 125, 122, 122],
...,
[ 66, 89, 49, ..., 2, 34, 40],
[ 44, 23, 29, ..., 40, 0, 16],
[ 29, 44, 84, ..., 17, 33, 18]], dtype=uint8)
---------------------------------------------
As above, I read picture from directory and index it to make picture red.
Because (960, 1280, 3) from jeju.shape is (height,width,rgb) and I thought that if I used [:,:,0], 0 meant red.( I thought r=0,g=1,b=2)
But result was not red picture but picture full of green and blue.
Why this thing happened? What [:,:,0] means in real?
You are right that it represents the red channel. However, the function imshow, from the official documentation stated that for a 2d array, The values are mapped to colors using normalization and a colormap.
If you want to plot your red channel only you can do this
red_image = np.zeros(np.shape(jeju))
red_image[:, :, 0] = jeju[:, :, 0]
plt.imshow(red_image.astype('uint8'))

Python thread and global interpreter lock when access same object

I am trying to understand python thread and how it works.
From my understanding, I know that there is GIL(Global Interpreter Lock) to prevent two thread to access memory at the same time.
This is pretty reasonable, even though it slows down the program.
But the code below, show unexpected result.
import thread, time
mylist = [[0,1]]
def listTo300Elem(id):
while len(mylist) < 300:
mylist.append([id, mylist[-1][1]+1])
thread.start_new_thread(listTo300Elem, (1,))
thread.start_new_thread(listTo300Elem, (2,))
thread.start_new_thread(listTo300Elem, (3,))
thread.start_new_thread(listTo300Elem, (4,))
thread.start_new_thread(listTo300Elem, (5,))
thread.start_new_thread(listTo300Elem, (6,))
thread.start_new_thread(listTo300Elem, (7,))
time.sleep(5)
print mylist
print len(mylist)
And the result is
[[0, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [1, 11], [1, 12], [1, 13], [1, 14], [1, 15], [1, 16], [1, 17], [1, 18], [1, 19], [1, 20], [1, 21], [1, 22], [1, 23], [1, 24], [1, 25], [1, 26], [1, 27], [1, 28], [1, 29], [1, 30], [1, 31], [1, 32], [1, 33], [1, 34], [1, 35], [1, 36], [1, 37], [1, 38], [1, 39], [1, 40], [1, 41], [1, 42], [1, 43], [1, 44], [1, 45], [1, 46], [1, 47], [1, 48], [1, 49], [1, 50], [1, 51], [1, 52], [1, 53], [1, 54], [1, 55], [1, 56], [1, 57], [1, 58], [1, 59], [1, 60], [1, 61], [1, 62], [1, 63], [1, 64], [1, 65], [1, 66], [1, 67], [1, 68], [1, 69], [1, 70], [1, 71], [1, 72], [2, 73], [2, 74], [2, 75], [2, 76], [2, 77], [2, 78], [2, 79], [5, 80], [5, 81], [5, 82], [5, 83], [5, 84], [5, 85], [5, 86], [5, 87], [5, 88], [5, 89], [5, 90], [5, 91], [5, 92], [5, 93], [5, 94], [3, 95], [3, 96], [3, 97], [3, 98], [3, 99], [3, 100], [3, 101], [3, 102], [3, 103], [3, 104], [3, 105], [3, 106], [3, 107], [3, 108], [3, 109], [3, 110], [3, 111], [3, 112], [3, 113], [3, 114], [3, 115], [3, 116], [3, 117], [7, 118], [7, 119], [7, 120], [7, 121], [7, 122], [7, 123], [7, 124], [2, 80], [2, 81], [2, 82], [2, 83], [2, 84], [2, 85], [2, 86], [2, 87], [2, 88], [2, 89], [2, 90], [2, 91], [2, 92], [2, 93], [2, 94], [2, 95], [2, 96], [2, 97], [2, 98], [2, 99], [2, 100], [2, 101], [2, 102], [2, 103], [2, 104], [2, 105], [2, 106], [2, 107], [2, 108], [2, 109], [2, 110], [2, 111], [2, 112], [2, 113], [2, 114], [2, 115], [2, 116], [2, 117], [2, 118], [2, 119], [2, 120], [2, 121], [2, 122], [2, 123], [2, 124], [2, 125], [2, 126], [2, 127], [2, 128], [2, 129], [2, 130], [2, 131], [2, 132], [2, 133], [2, 134], [2, 135], [2, 136], [2, 137], [2, 138], [2, 139], [2, 140], [2, 141], [7, 125], [7, 126], [7, 127], [7, 128], [7, 129], [7, 130], [7, 131], [7, 132], [7, 133], [7, 134], [7, 135], [7, 136], [7, 137], [7, 138], [7, 139], [7, 140], [7, 141], [7, 142], [7, 143], [7, 144], [7, 145], [7, 146], [7, 147], [7, 148], [7, 149], [7, 150], [7, 151], [7, 152], [7, 153], [7, 154], [7, 155], [7, 156], [7, 157], [7, 158], [7, 159], [7, 160], [7, 161], [7, 162], [7, 163], [7, 164], [7, 165], [7, 166], [7, 167], [7, 168], [7, 169], [7, 170], [7, 171], [6, 172], [6, 173], [6, 174], [6, 175], [6, 176], [6, 177], [6, 178], [3, 179], [3, 180], [3, 181], [3, 182], [3, 183], [3, 184], [3, 185], [3, 186], [3, 187], [3, 188], [3, 189], [3, 190], [3, 191], [3, 192], [3, 193], [3, 194], [3, 195], [3, 196], [3, 197], [3, 198], [3, 199], [3, 200], [3, 201], [7, 202], [7, 203], [7, 204], [7, 205], [7, 206], [7, 207], [7, 208], [7, 209], [1, 210], [1, 211], [1, 212], [1, 213], [1, 214], [1, 215], [1, 216], [1, 217], [1, 218], [1, 219], [1, 220], [1, 221], [1, 222], [1, 223], [1, 224], [1, 225], [1, 226], [1, 227], [1, 228], [1, 229], [1, 230], [1, 231], [1, 232], [1, 233], [1, 234], [1, 235], [1, 236], [1, 237], [1, 238], [3, 239], [5, 240], [2, 142], [6, 179]]
304
From my understanding, the result must be in order due to GIL, but they are not.
Can I get explanation for this example and further items to study?
What did you expect? That one thread add one item, after that another thread and etc.? Than why so many threads, if they work by one at once? Threads are trying to work simultaneously with one object. But since the GIL is not a good thing to do parallel computing, they do it so ugly.
To get more undestanding how GIL works, you may add logging.
logging.basicConfig(format="%(levelname)-8s [%(asctime)s] %(threadName)-12s %(message)s", level=logging.DEBUG, filename='log.log')
def listTo300Elem(id):
list_len = len(mylist)
while list_len < 300:
item = mylist[-1][1]+1]
mylist.append([id, item])
logging.debug('Len = {}, item {} added'.format(list_len, item))
list_len = len(mylist)
logging.debug('Len = {}, exit'.format(list_len, item))
So, threading in python is not suitable for all cases.

pandas: merge on column of ByteArray

Any ideas on how I can join two pandas arrays on a commonly named bytearray field? The field in the source (Teradata) is an actual ByteArray, and from the Teradata Side, this cannot be forced to character or something usable outside of Teradata)
The Teradata Export reads into a Panda's array beautifully. But I can't merge two tables with a commonly named field (DatabaseId) where that field is a bytearray.
(importing both pandas as pd and itertools)
When I try a simple merge of:
merge1 = pd.merge(tvm, dbase, on="DatabaseId")
I get the error of:
TypeError: type object argument after * must be a sequence, not itertools.imap
I searched StackOverflow and found a similar problem for joining on a cell containing a collection
dbase['DBID'] = dbase.DatabaseId.apply(lambda r: type(sorted(r.iteritems())))
But I get the Error:
AttributeError: 'bytearray' object has no attribute 'iteritems'
UPDATE
Example of Data
Data gathered through pandas using
dbase = pd.read_sql('select databaseid, databasename from ud812.dbase sample 10', conn)
conn is a connection to a teradata database
Data Types coming out of Teradata are Varchar for all columns EXCEPT:
DatabaseID = bytearray (Byte(4))
TVMID = bytearray (Byte(4))
>>> dbase.dtypes
DatabaseId object
DatabaseName object
dtype: object
>>> dbase
DatabaseId DatabaseName
0 [2, 0, 243, 185] PCDW_CRS_BBCONV3_TB
1 [2, 0, 168, 114] PAMLIF_TB
2 [2, 0, 133, 153] PADW_PRESN_TB
3 [2, 0, 29, 184] CEDW_MOBILE_TB
4 [2, 0, 190, 183] CEDW_MODEL_SCORE_TB
5 [2, 0, 71, 55] PBBBAM_TB
6 [2, 0, 169, 183] CEDW_OCC_TB
7 [2, 0, 201, 183] CCDW_DGTL_DEAL_TB
8 [0, 0, 139, 8] PRECDSS_TB
9 [2, 0, 142, 203] CDBDW_TB
>>>
>>>
>>> tvm.dtypes
TVMId object
DatabaseId object
TVMName object
TableKind object
CreateText object
dtype: object
>>> tvm
TVMId DatabaseId TVMName \
0 [230, 1, 41, 11, 0, 0] [2, 0, 67, 183] JCP_03538_112002
1 [214, 1, 60, 133, 0, 0] [2, 0, 186, 52] STL_AUTHNCTD_RULE_EXECN
2 [193, 2, 59, 48, 0, 0] [2, 0, 225, 150] uye177_Xsell_EM_OPCL_TB2
3 [0, 2, 235, 154, 0, 0] [2, 0, 244, 181] PL_CALCD_INVSTR_MTHLY_HIST_ST
4 [255, 1, 131, 76, 0, 0] [2, 0, 110, 63] IMH867_AVA0803_SNAP
5 [125, 1, 217, 138, 0, 0] [2, 0, 237, 153] FD_ACCT_STMT_ADR_ST
6 [224, 0, 80, 233, 0, 0] [2, 0, 243, 127] EXP_SRCH_RSLT_DESC
7 [208, 1, 72, 15, 0, 0] [2, 0, 8, 57] SGI_PAY_DENIED_SEP_112012
8 [246, 0, 27, 61, 0, 0] [2, 0, 143, 130] CR_INDIVD
9 [186, 1, 242, 167, 0, 0] [0, 0, 244, 18] wzu448_sb_apps
TableKind CreateText
0 T None
1 V CREATE VIEW ... ... ... ... ... ... ... ... ...
2 T None
3 V CREATE VIEW ... ... ... ... ... ... ... ... ...
4 T None
5 V CREATE VIEW ... ... ... ... ... ... ... ... ...
6 V CREATE VIEW ... ... ... ... ... ... ... ... ...
7 V CREATE VIEW ... ... ... ... ... ... ... ... ...
8 V CREATE VIEW ... ... ... ... ... ... ... ... ...
9 T None
Convert your bytearrays to their immutable cousin bytes.
import pandas as pd
# Create your example `dbase`
DatabaseId_dbase = list(map(bytearray, [[2, 0, 243, 185], [2, 0, 168, 114],
[2, 0, 133, 153], [2, 0, 29, 184], [2, 0, 190, 183], [2, 0, 71, 55],
[2, 0, 169, 183], [2, 0, 201, 183], [0, 0, 139, 8], [2, 0, 142, 203]]))
DatabaseName = ['PCDW_CRS_BBCONV3_TB', 'PAMLIF_TB', 'PADW_PRESN_TB',
'CEDW_MOBILE_TB', 'CEDW_MODEL_SCORE_TB', 'PBBBAM_TB', 'CEDW_OCC_TB',
'CCDW_DGTL_DEAL_TB', 'PRECDSS_TB', 'CDBDW_TB']
dbase = pd.DataFrame({'DatabaseId': DatabaseId_dbase,
'DatabaseName': DatabaseName})
# Create your example `tvm`
DatabaseId_tvm = list(map(bytearray, [[2, 0, 67, 183], [2, 0, 186, 52],
[2, 0, 225, 150], [2, 0, 244, 181], [2, 0, 110, 63], [2, 0, 237, 153],
[2, 0, 243, 127], [2, 0, 243, 185], [2, 0, 143, 130], [0, 0, 244, 18]]))
TVMId = list(map(bytearray, [[230, 1, 41, 11, 0, 0], [214, 1, 60, 133, 0, 0],
[193, 2, 59, 48, 0, 0], [0, 2, 235, 154, 0, 0], [255, 1, 131, 76, 0, 0],
[125, 1, 217, 138, 0, 0], [224, 0, 80, 233, 0, 0], [208, 1, 72, 15, 0, 0],
[246, 0, 27, 61, 0, 0], [186, 1, 242, 167, 0, 0]]))
TVMName = ['JCP_03538_112002', 'STL_AUTHNCTD_RULE_EXECN',
'uye177_Xsell_EM_OPCL_TB2', 'PL_CALCD_INVSTR_MTHLY_HIST_ST',
'IMH867_AVA0803_SNAP', 'FD_ACCT_STMT_ADR_ST', 'EXP_SRCH_RSLT_DESC',
'SGI_PAY_DENIED_SEP_112012', 'CR_INDIVD', 'wzu448_sb_apps']
TableKind = ['T', 'V', 'T', 'V', 'T', 'V', 'V', 'V', 'V', 'T']
tvm = pd.DataFrame({'DatabaseId': DatabaseId_tvm, 'TVMId': TVMId,
'TVMName': TVMName, 'TableKind': TableKind})
# This line would fail with the following error
# TypeError: type object argument after * must be a sequence, not map
# merge = pd.merge(tvm, dbase, on='DatabaseId')
# Apply the `bytes` constructor to the `bytearray` columns
dbase['DatabaseId'] = dbase['DatabaseId'].apply(bytes)
tvm['DatabaseId'] = tvm['DatabaseId'].apply(bytes)
tvm['TVMId'] = tvm['TVMId'].apply(bytes)
# Now it works!
merge = pd.merge(tvm, dbase, on='DatabaseId')
The resulting merge is
DatabaseId TVMId TVMName \
0 b'\x02\x00\xf3\xb9' b'\xd0\x01H\x0f\x00\x00' SGI_PAY_DENIED_SEP_112012
TableKind DatabaseName
0 V PCDW_CRS_BBCONV3_TB
(I had to change the DatabaseId field of one of the rows in your tvm, since otherwise merge would have been empty. I also didn't include the CreateText column — too awkward for SO)

Categories