Categories
Python Answers

How to get the current time with Python?

Sometimes, we want to get the current time with Python.

In this article, we’ll look at how to get the current time with Python.

How to get the current time with Python?

To get the current time with Python, we can use datetime.datetime.now().time().

For instance, we write

t = datetime.datetime.now().time()

to get the current datetime with datetime.datetime.now().

And then we get the time of the current datetime with time.

Conclusion

To get the current time with Python, we can use datetime.datetime.now().time().

Categories
Python Answers

How to calculate time difference between two Pandas columns in hours and minutes with Python?

Sometimes, we want to calculate time difference between two Pandas columns in hours and minutes with Python.

In this article, we’ll look at how to calculate time difference between two Pandas columns in hours and minutes with Python.

How to calculate time difference between two Pandas columns in hours and minutes with Python?

To calculate time difference between two Pandas columns in hours and minutes with Python, we can use the Timedelta class.

For instance, we write

t1 = pd.to_datetime('1/1/2015 01:00')
t2 = pd.to_datetime('1/1/2015 03:30')

print(pd.Timedelta(t2 - t1).seconds / 3600.0)

to create 2 Pandas datetimes with t1 and t2.

And then we subtract both datetimes and put the difference in the Timedelta class.

And then we get the difference in seconds with the seconds property.

We divide the seconds by 3600.0 to get the difference in hours.

Conclusion

To calculate time difference between two Pandas columns in hours and minutes with Python, we can use the Timedelta class.

Categories
Python Answers

How to convert binary to ASCII and vice versa with Python?

Sometimes, we want to convert binary to ASCII and vice versa with Python.

In this article, we’ll look at how to convert binary to ASCII and vice versa with Python.

How to convert binary to ASCII and vice versa with Python?

To convert binary to ASCII and vice versa with Python, we can use the bin function to convert ASCII to binary.

For instance, we write

b = bin(int.from_bytes('hello'.encode(), 'big'))

to call int.from_bytes with the 'hello' string to create an int from the string.

Then we call bin to convert the int to bytes.

To convert binary to ASCII, we call to_bytes.

For instance, we write

n = int('0b110100001100101011011000110110001101111', 2)
s = n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()

to convert the binary string to an int.

Then we call to_bytes with the bit_length with (n.bit_length() + 7) // 8 and 'big' to convert the int to bytes.

And then we call decode to bytes back to a string.

Conclusion

To convert binary to ASCII and vice versa with Python, we can use the bin function to convert ASCII to binary.

Categories
Python Answers

How to serialize a Decimal object into JSON with Python?

Sometimes, we want to serialize a Decimal object into JSON with Python.

In this article, we’ll look at how to serialize a Decimal object into JSON with Python.

How to serialize a Decimal object into JSON with Python?

To serialize a Decimal object into JSON with Python, we can use the json.dumps method.

For instance, we write

json.dumps(Decimal('3.9'))

to call json.dumps with a Decimal object.

It returns the string with the Decimal number value.

Conclusion

To serialize a Decimal object into JSON with Python, we can use the json.dumps method.

Categories
Python Answers

How to use the apply() function for a single column with Python Pandas?

Sometimes, we want to use the apply() function for a single column with Python Pandas.

In this article, we’ll look at how to use the apply() function for a single column with Python Pandas.

How to use the apply() function for a single column with Python Pandas?

To use the apply() function for a single column with Python Pandas, we can call apply with a lambda function.

For instance, we write

df['a'] = df['a'].apply(lambda x: x + 1)

to call apply on the column a on data frame df with a function that adds 1 to each entry in column a.

Then we assign the retuned values back to column a.

Conclusion

To use the apply() function for a single column with Python Pandas, we can call apply with a lambda function.