Categories
Python Answers

How to test whether a Numpy array contains a given row with Python?

Sometimes, we want to test whether a Numpy array contains a given row with Python.

In this article, we’ll look at how to test whether a Numpy array contains a given row with Python.

How to test whether a Numpy array contains a given row with Python?

To test whether a Numpy array contains a given row with Python, we can use the tolist method.

For instance, we write

a = np.array([[1,2],[10,20],[100,200]])
in_list = [1, 2] in a.tolist()

to create an array with nested lists with np.array.

Then we can check if [1, 2] is in the list that’s returned by a.tolist which converts the NumPy array a back to to the original nested list.

And finally, we use the in operator to check if [1, 2] is in the nested list returned by tolist which should return True since [1, 2] is in the original list that we used to create the a array.

Conclusion

To test whether a Numpy array contains a given row with Python, we can use the tolist method.

Categories
Python Answers

How to convert string to datetime format in Pandas Python?

Sometimes, we want to convert string to datetime format in Pandas Python.

In this article, we’ll look at how to convert string to datetime format in Pandas Python.

How to convert string to datetime format in Pandas Python?

To convert string to datetime format in Pandas Python, we can use the astype method.

For instance, we write

updated_df = df['timestamp'].astype('datetime64[ns]')

to get the timestamp column with df['timestamp'].

Then we call astype with 'datetime64[ns]' to convert the timestamp column values to datetimes.

Conclusion

To convert string to datetime format in Pandas Python, we can use the astype method.

Categories
Python Answers

How to fix DeprecationWarning: executable_path has been deprecated with Selenium Python?

Sometimes, we want to fix DeprecationWarning: executable_path has been deprecated with Selenium Python.

In this article, we’ll look at how to fix DeprecationWarning: executable_path has been deprecated with Selenium Python.

How to fix DeprecationWarning: executable_path has been deprecated with Selenium Python?

To fix DeprecationWarning: executable_path has been deprecated with Selenium Python, we upgrade selenium to version 4.

Then we add the webdriver_manager package.

To do both, we run

pip3 install -U selenium
pip3 install webdriver_manager

Then we update the code by writing

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.example.com")

to create the driver with

webdriver.Chrome(service=Service(ChromeDriverManager().install()))

Then we call get to open the page at the given URL.

Conclusion

To fix DeprecationWarning: executable_path has been deprecated with Selenium Python, we upgrade selenium to version 4.

Then we add the webdriver_manager package.

Categories
Python Answers

How to access class property from string with Python?

Sometimes, we want to access class property from string with Python.

In this article, we’ll look at how to access class property from string with Python.

How to access class property from string with Python?

To access class property from string with Python, we can use the setattr method to set attribute values and getattr to get attribute values.

For instance, we write

class C:
    pass

o = C()

setattr(o, "foo", "bar")
v = getattr(o, "foo")

to create class C.

Then we create an instance of C and assign it to o.

Next, we call setattr to add the o.foo attribute and set it to 'bar'.

And then we call getattr to get the value of the o.foo property.

Conclusion

To access class property from string with Python, we can use the setattr method to set attribute values and getattr to get attribute values.

Categories
Python Answers

How to do list rotation with Python?

Sometimes, we want to do list rotation with Python.

In this article, we’ll look at how to do list rotation with Python.

How to do list rotation with Python?

To do list rotation with Python, we can use list slicing.

For instance, we write

def rotate(l, n):
    return l[n:] + l[:n]

to create the rotate function that rotates list l for n positions.

In it, we get the slice of the array from index n to the end of the array.

And we append the list l from index 0 to the index n - 1 after the first list slice.

Conclusion

To do list rotation with Python, we can use list slicing.