Categories
Python Answers

How to store different datatypes in one NumPy array with Python?

Sometimes, we want to store different datatypes in one NumPy array with Python.

In this article, we’ll look at how to store different datatypes in one NumPy array with Python.

How to store different datatypes in one NumPy array with Python?

To store different datatypes in one NumPy array with Python, we can store the values in a record array.

For instance, we write

a = numpy.array(['a', 'b', 'c', 'd', 'e'])
b = numpy.arange(5)
records = numpy.rec.fromarrays((a, b), names=('keys', 'data'))

to call numpr.rec.fromarrays with (a, b) and the names argument set to a tuple with the column names.

Then we get a NumPy record array that has a list of items with the tuples for each item at the given position in each tuple.

We can then get the values with the keys like

keys = records['keys']

to get the values in a.

Conclusion

To store different datatypes in one NumPy array with Python, we can store the values in a record array.

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.